How to Generate Barcode image in CakePHP

, , Leave a comment

How to Generate Barcode image in Cakephp
In this easy and tutorial we are going to learn how to generate Barcode image in CakePHP. In CakePHP we have Barcode Helper class to fulfill this. This helper can generate different Barcodes. This Helper has following features.

Supported images :
•    PNG (default)
•    GIF
•    JPEG

Supported barcodes :
•    Code 128 (Set B et C)
•    Code 25 standard et code 25 Entrelacé
•    Code MSI
•    Code 39
•    Code 11
•    Code KIX
•    Code CMC7
•    Code PostFix
•    Cadabar
•    Code UPC / EAN 8 et 13

Methods summary of Helper class:

•  setCode($code) : specify the data to encode in barcode
•  setType($type) : type of barcode : EAN, UPC, C39…
•  setSize($h, $w=0, $cz=0) : size of the image

• setText($text=’AUTO’) : Text under the barcode ‘AUTO’ -> display the code value ” -> no text displayed ‘string’ -> display ‘string’ under the barcode 

•  hideCodeType() : Hide code type on barcode image (EAN, C128…)
•  setColors($fg, $bg=0) : foreground and background colors
•  setFiletype($ft) : filetype to generate : PNG (default), GIF, JPG

Recommended Reading : Creating RSS Feed in CakePHP

How to use it?

To generate Barcode using this helper class follow the steps given below:

(1) Download the Barcode Helper From Here : Barcode Helper

(2) Make one folder called “barcode” in app/webroot/img to store the generated barcode image.You can store anywhere you want, this is what i have done.

(3) Put the downloaded helper class (BarcodeHelper.php) file in the app/Vendor folder.

(4) Add following line at the top of the controller file.

App::uses(‘BarcodeHelper’,’Vendor’);

<?php
App::uses('BarcodeHelper','Vendor');
class PostsController extends AppController{
var $name = 'Posts';

(5) Copy the following code and paste it in the add function (before the save call ) of your controller for which you want to generate barcode.

// sample data to encode
$data_to_encode = '1012012,BLAHBLAH01234,1234567891011';

$barcode=new BarcodeHelper();

// Generate Barcode data
$barcode->barcode();
$barcode->setType('C128');
$barcode->setCode($data_to_encode);
$barcode->setSize(80,200);

// Generate filename
$random = rand(0,1000000);
$file = 'img/barcode/code_'.$random.'.png';

// Generates image file on server
$barcode->writeBarcodeFile($file);



UPDATE
If you want to make the generated barcode image transparent, then add the below extra code:

// making the image transparent Start //
$picture = imagecreatefrompng(WWW_ROOT.$file);
$img_w = imagesx($picture);
$img_h = imagesy($picture);

$newPicture = imagecreatetruecolor( $img_w, $img_h );
imagesavealpha( $newPicture, true );
$rgb = imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 );
imagefill( $newPicture, 0, 0, $rgb );
$color = imagecolorat( $picture, $img_w-1, 1);

for( $x = 0; $x < $img_w; $x++ ) {
for( $y = 0; $y < $img_h; $y++ ) {
$c = imagecolorat( $picture, $x, $y );
if($color!=$c){
imagesetpixel( $newPicture, $x, $y, $c);
}
}
}

imagepng($newPicture, WWW_ROOT.'imgbarcodetest.png');
imagedestroy($newPicture);
imagedestroy($picture);
// END

If you want to check that image is really transparent or not, then just add the below code in you function’s .ctp file (here add.ctp file).So you can see that image will be displayed as a transparent.

<div style="background: red;">
<?php echo $this->Html->image('barcode/test.png', array('alt' => 'CakePHP')); ?>
</div>

That’s it…

Just run your add functionality and check for the generated barcode image in its folder.(app/webroot/img/barcode in this case).

If you have any query or any problem in implementation then please mail me. I will help.