Error while using while to generate barcode

Asked

Viewed 100 times

1

First:

I’m using the function below and am implementing a chile.

Reason: need to generate several codes, example: 1 2 3 4 5 .... 98 99 100,follows code:

<?php
 $i = 1;
while ($i <= 100) {

    function geraCodigoBarra($numero){
        $fino = 1;
        $largo = 3;
        $altura = 70;

        $barcodes[0] = '00110';
        $barcodes[1] = '10001';
        $barcodes[2] = '01001';
        $barcodes[3] = '11000';
        $barcodes[4] = '00101';
        $barcodes[5] = '10100';
        $barcodes[6] = '01100';
        $barcodes[7] = '00011';
        $barcodes[8] = '10010';
        $barcodes[9] = '01010';

        for($f1 = 9; $f1 >= 0; $f1--){
            for($f2 = 9; $f2 >= 0; $f2--){
                $f = ($f1*10)+$f2;
                $texto = '';
                for($i = 1; $i < 6; $i++){
                    $texto .= substr($barcodes[$f1], ($i-1), 1).substr($barcodes[$f2] ,($i-1), 1);
                }
                $barcodes[$f] = $texto;
            }
        }

        echo '<img src="imagens/p.gif" width="'.$fino.'" height="'.$altura.'" border="0" />';
        echo '<img src="imagens/b.gif" width="'.$fino.'" height="'.$altura.'" border="0" />';
        echo '<img src="imagens/p.gif" width="'.$fino.'" height="'.$altura.'" border="0" />';
        echo '<img src="imagens/b.gif" width="'.$fino.'" height="'.$altura.'" border="0" />';

        echo '<img ';

        $texto = $numero;

        if((strlen($texto) % 2) <> 0){
            $texto = '0'.$texto;
        }

        while(strlen($texto) > 0){
            $i = round(substr($texto, 0, 2));
            $texto = substr($texto, strlen($texto)-(strlen($texto)-2), (strlen($texto)-2));

            if(isset($barcodes[$i])){
                $f = $barcodes[$i];
            }

            for($i = 1; $i < 11; $i+=2){
                if(substr($f, ($i-1), 1) == '0'){
                    $f1 = $fino ;
                }else{
                    $f1 = $largo ;
                }

                echo 'src="imagens/p.gif" width="'.$f1.'" height="'.$altura.'" border="0">';
                echo '<img ';

                if(substr($f, $i, 1) == '0'){
                    $f2 = $fino ;
                }else{
                    $f2 = $largo ;
                }

                echo 'src="imagens/b.gif" width="'.$f2.'" height="'.$altura.'" border="0">';
                echo '<img ';
            }
        }
        echo 'src="imagens/p.gif" width="'.$largo.'" height="'.$altura.'" border="0" />';
        echo '<img src="imagens/b.gif" width="'.$fino.'" height="'.$altura.'" border="0" />';
        echo '<img src="imagens/p.gif" width="1" height="'.$altura.'" border="0" />';
        echo'<br>';
        echo'<br>';
        echo"EAN: $i";
    }

    geraCodigoBarra('$i');

    echo $i++;

    }
?>

Return:

EAN: 111 Fatal error: Cannot redeclare geraCodigoBarra() (Previously declared in C: xampp htdocs cb index.php:10) in C: xampp htdocs cb index.php on line 10

Second: After generating these codes, it is possible to export them separately to . jpg ? to a given folder? , if yes how could I do.

Finally:

I need the code to be 13 numbers,: 1 in 0000000000001

  • Where does $numero come from?

  • Since you are using PHP, perhaps it would be better to use the GD library and make the bars in one image. It’s out of the question, but the bar algorithm 2 of 5 can be simplified as well.

  • It would be good not to mix several doubts of a different nature in the same question. Usually it is more effective to ask separately, solving one problem at a time and moving on to the next in another post. This makes it easier for respondents, allows different people with specific knowledge to take better care of each subject, and decreases the chance of amplitude closure.

1 answer

3


Friend, the generating functionCodigoBarra($number) it needs to be out of the while, because it is a loop, it is processing repeatedly so the error saying that you can’t redeclare the function (Cannot redeclare).

<?php
function geraCodigoBarra($numero){
// seu codigo aqui
}


$i = 1;
while ($i <= 100) {

// processa a funcao
geraCodigoBarra('$i');

echo $i++;

}
?>

Referring to your questions, please visit the: http://php.net/manual/en/ and search for the functions I pass below:

Refer to image creation use function:

imagecreate

To save to folder, use file manipulation functions:

http://php.net/manual/en/book.filesystem.php

  • mkdir - creates the directory
  • fopen - to create the archive
  • file_put_contents - to write the image data to the file
  • fclose - closes the file you wrote

About the size that needs output, see str_pad function .

Obs: everything you need you find in Google, a search, but the links I passed are manual, easy to do. But there are classes ready that generate barcodes already with save functions as you want.

  • Perfect, helped a lot, Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.