Skip to main content

create bar code in php

Create bar code in php 


  <?php
function createBarcode() {
       
        $height = 52;
        $width = 102;

        $lines = array();

        $lines[0] = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
        $lines[1] = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
        $lines[2] = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
        $lines[3] = "000000000000100011111001111000000100001110000111000010000001110000100000011100000010001111100000000000";
        $lines[4] = "000000000000100010000000000100000100000001001000100010000010001000010000100010000010001000000000000000";
        $lines[5] = "000000000000100001000000000100111110000001001000100010000010001000001000100010000010000100000000000000";
        $lines[6] = "000000000000100000100000000100100100000001001000100010000010001000111100100010000010000010000000000000";
        $lines[7] = "000000000000100000010000111000010100001110001111000001000001110001000100100010000010000001000000000000";
        $lines[8] = "000000000010100000001000010000010000001000000100000000100010001001000100100010001010000000100000000000";
        $lines[9] = "000000000001100010001000001000001000001000000010000000010010001001000100100010000110001000100000000000";
        $lines[10] = "000000000000100001110001111100001000001111000001000111110001110000111000011100000010000111000000000000";
        $lines[11] = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
        $lines[12] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[13] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[14] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[15] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[16] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[17] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[18] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[19] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[20] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[21] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[22] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[23] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[24] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[25] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[26] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[27] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[28] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[29] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[30] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[31] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[32] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[33] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[34] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[35] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[36] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[37] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[38] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[39] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[40] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[41] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[42] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[43] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[44] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[45] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[46] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[47] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[48] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[49] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[50] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[51] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";
        $lines[52] = "110100111001011001110010001011000111000101101100001010011011110110101100111001110101100011000111010110";

        $lines2 = array();

        $count = count($lines);

for( $i=0; $i< $count; $i++ )
            $lines2[$i] = tstr($lines[$i]);

        writeBarcode(getHeader($width, $height) . implode('', $lines2));
    }

    function tstr($data ) {

  static $tStrCache;

if( isset( $tStrCache[$data] ))
return $tStrCache[$data];

        $tchar = 0;
        $total = "";
$tpos = 8;

        for( $i = 0; $i < strlen($data); $i++ ) {
            $tpos = $tpos - 1;
if( substr($data, $i, 1) !== '0' )
$tchar = $tchar + pow(2, $tpos);

if( $tpos == 0 ) {
$total = $total . chr($tchar);
$tpos = 8;
$tchar = 0;
}
}
        if( $tpos != 8 ) {
            $total = $total . chr($tchar);
}

        $x = strlen($total) % 4;
if( $x == 0 )
$x = 4;

        for( $i = $x; $i<=3; $i++ ) {
            $total = $total . chr(0);
    }

        $tStrCache[$data] = $total;
return $total;
    }

    function size($length) {

        $length = (double)$length;

        if ( $length > 255 ) {

   if(  $length > 65535 )
$length = 65535;

            return chr($length % 256) . chr(floor($length / 256));
} else {
            return chr($length) . chr(0);
        }
    }

    function getHeader($width, $height) {
        return chr(66) . chr(77) . size(62 + ($width * $height)) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(62) . chr(0) . chr(0) . chr(0) . chr(40) . chr(0) . chr(0) . chr(0) . size($width) . chr(0) . chr(0) . size($height) . chr(0) . chr(0) . chr(1) . chr(0) . chr(1) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(37) . chr(14) . chr(0) . chr(0) . chr(37) . chr(14) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(255) . chr(255) . chr(255) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0);
    }
 
    function writeBarcode($data) {
        header( "Content-Type: image/bmp" );
    echo $data;
    }
   
    createBarcode();

Comments

Popular posts from this blog

how send mail using Swift mailer | switmailer send mail tutorial

how send mail using Swift mailer | switmailer send mail tutorial  <?php /* step 1 include Swift mailer file ;  */ include("vendor/swiftmailer/swiftmailer/lib/swift_required.php"); /* step 2 create your user details */ $senderEmail="saddam1234321@gmail.com"; $senderPassword="****Your password****"; $senderName="Saddam huassin"; /* step 3 set SMTP host  for sending mail  */ $user = Swift_SmtpTransport::newInstance('smtp.gmail.com','587','tls'); /* step 4 set your user details  */ $user->setUsername($senderEmail); $user->setPassword($senderPassword); /* step 5 create swift  mailer instance with a variable */ $mailer = Swift_Mailer::newInstance($user); /* step 6 Create a message */ $message = Swift_Message::newInstance('Wonderful Subject')   ->setFrom(array($senderEmail => $senderName))   ->setTo(array($senderEmail))   ->setBody('Here is the message itsel...

how install Swift mailer in localhost | swift mailer installation | composer install swift mailer

how install Swift mailer in localhost | swift mailer installation | composer install swift mailer Step 1:- open cmd  Step 2;- copy this command " composer required swiftmailer/swiftmailer @stable " . Step 3;- paste in cmd and press enter. Step 4;- after few second swift mailer ready to use .................................