[PHP] barcode class
Solved
Tantor8
Posted messages
46
Status
Membre
-
titou38 -
titou38 -
Hello,
I need to generate a PDF containing images of barcodes. For this, I am using fpdf and a class found on pitoo.com: http://pit.free.fr/code_php.php
Very well done and fully functional, I tested this class in a file and as it is written on their site, you just have to do:
to have an image in our page.
But to insert it into the PDF, I don't see how to do it. I've tried:
And for the first imagepng I have the following error:
Warning: imagepng(): supplied argument is not a valid Image resource in c:\program files\easyphp1-8\www\toutpack\format_pdf.php on line 169
And for the $pdf->Image($file... I have:
Warning: fopen(./CodeBarre/.png): failed to open stream: No such file or directory in c:\program files\easyphp1-8\www\toutpack\classfpdf\fpdf.php on line 1522
FPDF error: Can't open image file: ./CodeBarre/.png
I imagine that the second one is necessarily due to the failure of creating the first.
I hope someone will be able to help me.
Thank you for your help.
--
A fool who walks will go further than a philosopher who sits!
I need to generate a PDF containing images of barcodes. For this, I am using fpdf and a class found on pitoo.com: http://pit.free.fr/code_php.php
Very well done and fully functional, I tested this class in a file and as it is written on their site, you just have to do:
echo "<img src=pi_barcode.php?type=EAN&code=".$_POST['codebarre12']."&height=50&readable=Y alt='no image'></img>";
to have an image in our page.
$_POST['codebarre12']being a 12-digit code since the class automatically creates the check digit.
But to insert it into the PDF, I don't see how to do it. I've tried:
$file = "./CodeBarre/$codebarre.png"; /*create the image*/ $image=imagepng("pi_barcode.php?type=EAN&code=124578569854&height=$hcode&readable=Y"); /*save the image in the current directory*/ imagepng($image, $file); /*insert the image into the pdf*/ $x=$pdf->getX(); $y=$pdf->getY(); $pdf->SetFillColor(255,187,255); $pdf->Cell(100,$hcode+10,"barcode",1,0,'C',$pdf->Image($file,$x,$y,0,$hcode,'PNG')); And for the first imagepng I have the following error:
Warning: imagepng(): supplied argument is not a valid Image resource in c:\program files\easyphp1-8\www\toutpack\format_pdf.php on line 169
And for the $pdf->Image($file... I have:
Warning: fopen(./CodeBarre/.png): failed to open stream: No such file or directory in c:\program files\easyphp1-8\www\toutpack\classfpdf\fpdf.php on line 1522
FPDF error: Can't open image file: ./CodeBarre/.png
I imagine that the second one is necessarily due to the failure of creating the first.
I hope someone will be able to help me.
Thank you for your help.
--
A fool who walks will go further than a philosopher who sits!
3 réponses
Re,
I slightly modified my code. To create the image, I am now using:
imagecreatefrompng creates the image from a source, either the file containing the class. Then imagepng saves it in a directory.
But I'm having the same problems as before:
Warning: imagecreatefrompng(pi_barcode.php?type=EAN&code=124578569854&height=12): failed to open stream: Invalid argument in c:\program files\easyphp1-8\www\toutpack\format_pdf.php on line 171
Warning: imagepng(): supplied argument is not a valid Image resource in c:\program files\easyphp1-8\www\toutpack\format_pdf.php on line 172
Warning: fopen(./CodeBarre/.png): failed to open stream: No such file or directory in c:\program files\easyphp1-8\www\toutpack\classfpdf\fpdf.php on line 1522
FPDF error: Can't open image file: ./CodeBarre/.png
Thank you for your help
--
A fool who walks will go further than a philosopher who sits!
I slightly modified my code. To create the image, I am now using:
$im = imagecreatefrompng("pi_barcode.php?type=EAN&code=124578569854&height=$hcode&readable=Y"); imagepng($im,$file); imagecreatefrompng creates the image from a source, either the file containing the class. Then imagepng saves it in a directory.
But I'm having the same problems as before:
Warning: imagecreatefrompng(pi_barcode.php?type=EAN&code=124578569854&height=12): failed to open stream: Invalid argument in c:\program files\easyphp1-8\www\toutpack\format_pdf.php on line 171
Warning: imagepng(): supplied argument is not a valid Image resource in c:\program files\easyphp1-8\www\toutpack\format_pdf.php on line 172
Warning: fopen(./CodeBarre/.png): failed to open stream: No such file or directory in c:\program files\easyphp1-8\www\toutpack\classfpdf\fpdf.php on line 1522
FPDF error: Can't open image file: ./CodeBarre/.png
Thank you for your help
--
A fool who walks will go further than a philosopher who sits!
Hello,
I am now able to save my barcode in a temporary .png file. For that, I followed this topic a bit:
https://www.developpez.net/forums/d546949/php/bibliotheques-frameworks/fpdf-fpdf-generateur-code-barres/#post3255401
You need to take what is under the class in pi_barcode and incorporate it into a method of the pdf in the php file that creates the pdf:
Then I call it when I want to include it in the pdf:
Additionally, it was necessary to modify the makeImage method in the pi_barcode class as follows:
I commented out the header because it causes problems otherwise and since the image is being generated anyway, I find it unnecessary.
The problem now is the fact that nothing appears in the generated pdf. Additionally, it has recently started showing the following error:
Notice: Undefined property: HR in c:\program files\easyphp1-8\www\toutpack\pi_barcode.php on line 469
I do not understand why it is not defined when it was working before.
If someone has a solution to my problem, thank you for helping me. Otherwise, I hope this will help others in the same case as me.
--
A fool who walks will go further than a philosopher who sits!
I am now able to save my barcode in a temporary .png file. For that, I followed this topic a bit:
https://www.developpez.net/forums/d546949/php/bibliotheques-frameworks/fpdf-fpdf-generateur-code-barres/#post3255401
You need to take what is under the class in pi_barcode and incorporate it into a method of the pdf in the php file that creates the pdf:
<?php include('pdf.php'); class PDF extends FPDF{ ... //generates the barcode function genCode($type, $code, $height, $readable, $width, $showtype){ include('pi_barcode.php'); $type = strtoupper($type); switch( $type ) { case "C128C" : if (preg_match("/^[0-9]{2,48}$/", $code)){ $tmp = strlen("$code"); if(($tmp%2)!=0) $code = "0$code"; } else { $type = "ERR"; $code = "CODE 128C REQUIRES DIGITS ONLY"; } case "C128" : $carok = true; for($i=0;$i<strlen($code);$i++) { $tmp = ord(substr($code,$i,1)) ; if($tmp < 32 OR $tmp > 126) $carok = false; } if( !$carok ) { $type = "ERR"; $code = "UNAUTHORIZED CHARS IN 128 CODE"; } break; case "UPC" : $code = "0$code"; case "EAN" : $long = strlen( $code ) ; $factor = 3; $sum = 0; if (preg_match("/^[0-9]{8}$/", $code) OR preg_match("/^[0-9]{13}$/", $code)){ // if ($long==13){ for ($index = ($long - 1); $index > 0; $index--) { $sum += substr( $code, $index - 1, 1 ) * $factor ; $factor = 4 - $factor ; } $cc = ( ( 1000 - $sum ) % 10 ) ; if ( substr( $code, -1, 1) != $cc ) { $type = "ERR"; $code = "CHECKSUM ERROR IN EAN/UPC CODE"; } } elseif (preg_match("/^[0-9]{7}$/", $code) OR preg_match("/^[0-9]{12}$/", $code)){ // } elseif ($long==12){ for ($index = ($long ); $index > 0; $index--) { $sum += substr( $code, $index - 1, 1 ) * $factor ; $factor = 4 - $factor ; } $cc = ( ( 1000 - $sum ) % 10 ) ; $code .= "$cc" ; } else { $type = "ERR"; $code = "THIS CODE IS NOT EAN/UPC TYPE"; } break; case "C25I" : $tmp = strlen("$code"); if(($tmp%2)==0) { $code = "0$code"; $tmp++; } case "C25" : if (preg_match("/^[0-9]{1,48}$/", $code)){ $checksum = 0; $factor = 3; $tmp = strlen("$code"); for($i=$tmp-1; $i>=0; $i--) { $checksum += (intval(substr($code,$i,1))*$factor); $factor = 4-$factor; } $checksum = 10-($checksum%10); if($checksum==10) $checksum = 0; $code .= "$checksum"; } else { $type = "ERR"; $code = "CODE C25 REQUIRES DIGITS ONLY"; } break; case "C39" : if(preg_match("/^[0-9A-Z\-\.\$\/+% ]{1,48}$/i", $code)) { $code = "*$code*"; } else { $type = "ERR"; $code = "UNAUTHORIZED CHARS IN CODE 39"; } break; case "CODABAR" : if(!preg_match("/^(A|B|C|D)[0-9\-\$:\/\.\+]{1,48}(A|B|C|D)$/i", $code)) { $type = "ERR"; $code = "CODABAR START/STOP : ABCD"; } break; case "MSI" : if (preg_match("/^[0-9]{1,48}$/", $code)){ $checksum = 0; $factor = 1; $tmp = strlen("$code"); for($i=0; $i<$tmp; $i++) { $checksum += (intval(substr($code,$i,1))*$factor); $factor++; if($factor > 10) $factor = 1; } $checksum = (1000-$checksum)%10; $code .= "$checksum"; } else { $type = "ERR"; $code = "CODE MSI REQUIRES DIGITS ONLY"; } break; case "C11" : if (preg_match("/^[0-9\-]{1,48}$/", $code)){ $checksum = 0; $factor = 1; $tmp = strlen("$code"); for($i=$tmp-1; $i>=0; $i--) { $tmp = substr($code,$i,1); if($tmp == "-") $tmp=10; else $tmp = intval($tmp); $checksum += ($tmp*$factor); $factor++; if($factor>10) $factor=1; } $checksum = $checksum%11; if($checksum==10) $code .= $checksum . "-"; else $code .= "$checksum"; } else { $type = "ERR"; $code = "UNAUTHORIZED CHARS IN CODE 11"; } break; case "POSTNET" : if (preg_match("/^[0-9]{5}$/", $code) OR preg_match("/^[0-9]{9}$/", $code) OR preg_match("/^[0-9]{11}$/", $code)){ $checksum = 0; $tmp = strlen("$code"); for($i=$tmp-1; $i>=0; $i--) { $checksum += intval(substr($code,$i,1)); } $checksum = 10-($checksum%10); if($checksum==10) $checksum=0; $code .= "$checksum"; } else { $type = "ERR"; $code = "POSTNET MUST BE 5/9/11 DIGITS"; } break; case "KIX" : if (preg_match("/^[A-Z0-9]{1,50}$/", $code)){ // ***** THE KIX CODE has no checksum (fix V2.02) // $checksum = 0; // $tmp = strlen("$code"); // for($i=$tmp-1; $i>=0; $i--) { // $checksum += intval(substr($code,$i,1)); // } // $checksum = 10-($checksum%10); // if($checksum==10) $checksum=0; // $code .= "$checksum"; } else { $type = "ERR"; $code = "UNAUTHORIZED CHARS IN KIX CODE"; } break; case "CMC7" : if(!preg_match("/^[0-9A-E]{1,48}$/", $code)) { $type = "ERR"; $code = "CMC7 MUST BE NUMERIC or ABCDE"; } break; default : $type = "ERR"; $code = "UNKNOWN BARCODE TYPE"; break; } // ***** Default width if( isset( $width ) && ( $width >= 10 ) ) { $hw = $width ; } else { $hw = 10 ; } // ***** Default height if( isset( $height ) && ( $height > 0 ) ) { $hh = $height ; } else { $hh = 10 ; } // ***** Other default values if( isset( $readable ) && ( $readable == "Y" ) ) { $hr = "Y" ; } else{ $hr = "N" ; } if( !isset( $showtype ) ) { $showtype = "N" ; } // ***** Create the object $objCode = new pi_barcode( $type, $code, $hh, $hr, $hw, $showtype ) ; $objCode -> makeImage() ; } } ?> Then I call it when I want to include it in the pdf:
/*option to display the numbers or not*/ if(isset($_POST['chiffre'])){ $pdf->genCode('EAN',$codebarre12, $hcode, 'Y', '', ''); }else{ $pdf->genCode('EAN',$codebarre12, $hcode, '', '', ''); } /*insert the image into the pdf*/ $file="codebarre.png"; $x=$pdf->setX(30); $y=$pdf->getY(); $pdf->SetFillColor(255,187,255); $pdf->Cell(100,$hcode+10,"barcode",1,0,'C',$pdf->Image($file,$x,$y,0,$hcode,'PNG')); Additionally, it was necessary to modify the makeImage method in the pi_barcode class as follows:
//Header( "Content-type: image/png" ); /*it displays the image imagepng($img);*/ //it saves the image imagepng($img,"codebarre.png"); //it destroys it imagedestroy($img); }//end of makeImage()
I commented out the header because it causes problems otherwise and since the image is being generated anyway, I find it unnecessary.
The problem now is the fact that nothing appears in the generated pdf. Additionally, it has recently started showing the following error:
Notice: Undefined property: HR in c:\program files\easyphp1-8\www\toutpack\pi_barcode.php on line 469
I do not understand why it is not defined when it was working before.
If someone has a solution to my problem, thank you for helping me. Otherwise, I hope this will help others in the same case as me.
--
A fool who walks will go further than a philosopher who sits!
I start and finish this topic then! lol
So for the HR error, I set the readable parameter to Y, so it always displays the barcode (the numbers + the lines). As for the image, I added a parameter in makeImage with the destination file name. This way each code has its own file. Previously, it was a common file that I thought I could use directly by inserting it into the pdf. The issue is that the pdf seems to be generated at the end of the page execution when we do the output. So at that moment, it only had the last image.
See you later
--
A fool who walks will go further than a sitting philosopher!
So for the HR error, I set the readable parameter to Y, so it always displays the barcode (the numbers + the lines). As for the image, I added a parameter in makeImage with the destination file name. This way each code has its own file. Previously, it was a common file that I thought I could use directly by inserting it into the pdf. The issue is that the pdf seems to be generated at the end of the page execution when we do the output. So at that moment, it only had the last image.
See you later
--
A fool who walks will go further than a sitting philosopher!