Here Now we have discuss how to generate barcode in PHP and use in different manner.I have taken reverence from ashberg blog.
First you download the barcode file From here :
When you save downloaded zip file and extract it.Now You are able to run this code .Here you have get file barcode.php which main index file for generating bar code ,which is show only the barcode generated image.
Option 1:May be You need to download the barcode generated image
Find the below code in php-barcode.php
- $mode=strtolower($mode);
- if ($mode==’jpg’ || $mode==’jpeg’){
- header(“Content-Type: image/jpeg; name=\”barcode.jpg\””);
- imagejpeg($im);
- } else if ($mode==’gif’){
- header(“Content-Type: image/gif; name=\”barcode.gif\””);
- imagegif($im);
- } else {
- header(“Content-Type: image/png; name=\”barcode.png\””);
- imagepng($im);
- }
And replace above code from nelow code for download image:
- if ($mode==’jpg’ || $mode==’jpeg’){
- header(“Content-Disposition: attachment; filename=p1.jpeg”);
- imagejpeg($im);
- } else if ($mode==’gif’){
- header(“Content-Type: image/gif; name=\”barcode.gif\””);
- imagegif($im);
- } else {
- header(“Content-Type: image/png; name=\”barcode.png\””);
- imagepng($im);
- }
Option 2:May be You need to save the image on folder
Then replace above code from below code,here You will see a$save location it’s the location where you save the generated barcode image.
- $mode=strtolower($mode);
- if ($mode==‘jpg’ || $mode==‘jpeg’){
- $save_loc =“uploads/barcode6.jpg”;
- echo”
- <h1>barcode created</h1>
- .$save_loc”;
- header(“Content-Type: image/jpeg; name=\”barcode.jpg\””);
- $save_loc =$save_loc.“barcode.jpg”;
- imagejpeg($im,$save_loc);
- } else if ($mode==‘gif’){
- $save_loc =“uploads/barcode6.gif”;
- echo”
- <h1>barcode created</h1>
- .$save_loc”;
- header(“Content-Type: image/gif; name=\”barcode.gif\””);
- $save_loc =$save_loc.“barcode.gif”;
- imagegif($im,$save_loc);
- } else {
- $save_loc =“uploads/barc_”.$image_name.“.png”;
- echo”
- <h1>barcode created</h1>
- Check at $save_loc”;
- header(“Content-Type: image/png; name=\”barcode.png\””);
- //$save_loc =$save_loc ;
- imagepng($im,$save_loc);
- }
Option 3:How to generate barcode with help of Curl .
1. First intilozed the curl with following code
- $ch = curl_init();
2. Now call the curl method with specific URL
- curl_setopt($ch, CURLOPT_URL,“http://localhost/magento/barcode/barcode.php?code=$strcode”);
3. Excute the file
- curl_exec ($ch);
4. Close the curl connection
- curl_close ($ch);