Advertisement
Guest User

Barcode_new

a guest
Jun 28th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.08 KB | None | 0 0
  1. <?php
  2.  
  3. function barcode( $filepath="", $text="0", $size="20", $orientation="horizontal", $code_type="code128", $print=false ) {
  4.     $white="white";
  5.     $black="black";
  6.     $code_string = "";
  7.     // Translate the $text into barcode the correct $code_type
  8.     if ( in_array(strtolower($code_type), array("code128", "code128b")) ) {
  9.         $chksum = 104;
  10.         // Must not change order of array elements as the checksum depends on the array's key to validate final code
  11.         $code_array = array(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","\`"=>"111422","a"=>"121124","b"=>"121421","c"=>"141122","d"=>"141221","e"=>"112214","f"=>"112412","g"=>"122114","h"=>"122411","i"=>"142112","j"=>"142211","k"=>"241211","l"=>"221114","m"=>"413111","n"=>"241112","o"=>"134111","p"=>"111242","q"=>"121142","r"=>"121241","s"=>"114212","t"=>"124112","u"=>"124211","v"=>"411212","w"=>"421112","x"=>"421211","y"=>"212141","z"=>"214121","{"=>"412121","|"=>"111143","}"=>"111341","~"=>"131141","DEL"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","FNC 4"=>"114131","CODE A"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
  12.         $code_keys = array_keys($code_array);
  13.         $code_values = array_flip($code_keys);
  14.         for ( $X = 1; $X <= strlen($text); $X++ ) {
  15.             $activeKey = substr( $text, ($X-1), 1);
  16.             $code_string .= $code_array[$activeKey];
  17.             $chksum=($chksum + ($code_values[$activeKey] * $X));
  18.         }
  19.         $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
  20.         $code_string = "211214" . $code_string . "2331112";
  21.     }
  22.      elseif ( strtolower($code_type) == "code39" ) {
  23.         $code_array = array("0"=>"111221211","1"=>"211211112","2"=>"112211112","3"=>"212211111","4"=>"111221112","5"=>"211221111","6"=>"112221111","7"=>"111211212","8"=>"211211211","9"=>"112211211","A"=>"211112112","B"=>"112112112","C"=>"212112111","D"=>"111122112","E"=>"211122111","F"=>"112122111","G"=>"111112212","H"=>"211112211","I"=>"112112211","J"=>"111122211","K"=>"211111122","L"=>"112111122","M"=>"212111121","N"=>"111121122","O"=>"211121121","P"=>"112121121","Q"=>"111111222","R"=>"211111221","S"=>"112111221","T"=>"111121221","U"=>"221111112","V"=>"122111112","W"=>"222111111","X"=>"121121112","Y"=>"221121111","Z"=>"122121111","-"=>"121111212","."=>"221111211"," "=>"122111211","$"=>"121212111","/"=>"121211121","+"=>"121112121","%"=>"111212121","*"=>"121121211");
  24.         // Convert to uppercase
  25.         $upper_text = strtoupper($text);
  26.         for ( $X = 1; $X<=strlen($upper_text); $X++ ) {
  27.             $code_string .= $code_array[substr( $upper_text, ($X-1), 1)] . "1";
  28.         }
  29.         $code_string = "1211212111" . $code_string . "121121211";
  30.     }
  31.  
  32.     // Pad the edges of the barcode
  33.     $code_length = 20;
  34.     if ($print) {
  35.         $text_height = 30;
  36.     } else {
  37.         $text_height = 0;
  38.     }
  39.    
  40.     for ( $i=1; $i <= strlen($code_string); $i++ ){
  41.         $code_length = $code_length + (integer)(substr($code_string,($i-1),1));
  42.         }
  43.     if ( strtolower($orientation) == "horizontal" ) {
  44.         $img_width = $code_length;
  45.         $img_height = $size;
  46.     } else {
  47.         $img_width = $size;
  48.         $img_height = $code_length;
  49.     }
  50.    
  51.     for ( $position = 1 ; $position <= strlen($code_string); $position++ ) {
  52.         //$cur_size = $location + ( substr($code_string, ($position-1), 1) );
  53.         $valoare = substr($code_string, ($position-1), 1);
  54.         if($valoare==1)
  55.             $cur_size = 0.5;
  56.         elseif($valoare==2)
  57.             $cur_size = 1;
  58.         elseif($valoare==3)
  59.             $cur_size = 1.5;
  60.         elseif($valoare==3)
  61.             $cur_size = 2;
  62.         if ( strtolower($orientation) == "horizontal" )
  63.             draw_bar($cur_size, ($position % 2 == 0 ? $white : $black) );
  64.         else
  65.             imagefilledrectangle( $image, 0, $location, $img_width, $cur_size, ($position % 2 == 0 ? $white : $black) );
  66.        
  67.     }
  68.    
  69. }
  70.  
  71. function draw_bar($cur_size, $color){
  72.  
  73.     ?>
  74.     <div style="width:<?php echo $cur_size?>; height:40px; background-color:<?php echo $color; ?>;float:left; position:relative"></div>
  75.     <?php
  76. }
  77.  
  78. ?>
  79.  
  80. <div id="cod_bare" style="width:1000px; margin:0 auto; height:300px;">
  81.     <?php barcode("", "RO503C001IP0722RZ001-050", "10", "horizontal", "code39", false ); ?>
  82. </div>
  83. <br><br><br><br><br><br><br><br><br><br><br><br>
  84. <div id="cod_bare" style="width:1000px; margin:0 auto; height:300px;">
  85.     <?php barcode("", "RO503C001IP0722RZ001-050", "10", "horizontal", "code128", false ); ?>
  86. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement