Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Generate Barcode Image in Cakephp 2

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 614
    Comment on it

    Generate Barcode Image in Cakephp 2

    Hello friends, today I am going to discuss how to generate Barcode image in Cakephp. In order to generate barcode image, Barcode Helper class is provided in Cakephp. Barcode Helper that we are going to use it can generate different Barcodes. It has following features:



    Supported images are:


    a) PNG (default)
    b) JPEG
    c) GIF


    Supported barcodes are:


    1)    Code MSI
    2)    Code 39
    3)    Code 128
    4)    Code 25 standard et code 25 Entrelaca
    5)    Code CMC7
    6)    Cadabar
    7)    Code 11
    8)    Code UPC / EAN 8 et 13
    9)    Code KIX
    10)  Code PostFix

    Summary of Helper class are:


    1) setCode($code) : This method will specify the data to encode in barcode
    2)  setType($type) : This method indicates the type of barcode ie. EAN, UPC & C39.
    3)  setSize($h, $w=0, $cz=0) : setSize is a method which will be used to know the size of the image.

    4) setText($text='AUTO') : Text under the barcode 'AUTO' will display the code value ''. '' will display no text.  'string' will display 'string' under the barcode.

    5) hideCodeType() : This method will hide the code type on barcode image (i.e. EAN, C128.)
    6) setColors($fg, $bg=0) : setColors method is used to set the foreground and background colors of barcode.
    7) setFiletype($ft) : This method will set the filetype to generate the PNG (default), GIF, JPG image.

     

    How to use Barcode?

    Follow the steps below in order to generate the barcode using this helper class:

    Step 1:  Firstly you need to download the Barcode Helper From this link : Barcode Helper

    Step 2: Now after downloading the Barcode Helper class, make new folder called “barcode” in app/webroot/img. This folder will be used to store the generated barcode image.

    Note: You can store barcode image anywhere you want to.

    Step 3: Next step is to copy the downloaded helper class (BarcodeHelper.php) file in the following folder: app/Vendor

    Step 4: After third step, add the following line at the top of the controller file.

    App::uses('BarcodeHelper','Vendor');

    For Example:

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




    Step 5: Now you need to copy the following code and paste it in the add function of your controller for which you want to generate barcode before the save call.

       

    // 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);

       
     
     

    To make the generated barcode image transparent, then you need to add the below 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.'img\barcode\test.png');
    imagedestroy($newPicture);
    imagedestroy($picture);



    If you want to see the transparent image is generated by this function or not then you need to add the lines of code in this add.ctp file. Now you will see the image is displayed as transparent:

     

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



    Now execute the code on the browser and you can check the generated barcode image in the following folder: app/webroot/img/barcode



    Thanks for reading.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: