首页 > 语言 > PHP > 正文

php生成条形码代码

2024-09-04 11:44:22
字体:大 中 小
来源:转载
供稿:网友

php生成条形码就是必须生成图片了,在php生成图片我们就必须用到gd库来实现了,所以你得找到你的将php.ini文件找到extension=php_gd2.dll 去掉前面的;,你就可以使用些实例了.

php生成条形码代码如下:

  1. class cd_barra 
  2. { 
  3.     var $file; 
  4.     var $into; 
  5.      
  6.     var $cd_barras = array(0=>"00110",1=>"10001",2=>"01001",3=>"11000",4=>"00101", 
  7.                            5=>"10100",6=>"01100",7=>"00011",8=>"10010",9=>"01010" 
  8.                            ); 
  9.     function cd_barra($value,$files,$into=1) {  
  10.       $lower = 1 ; $hight = 55;           
  11.       $this->into = $into; 
  12.       for($count1=9;$count1>=0;$count1--){  
  13.         for($count2=9;$count2>=0;$count2--){    
  14.           $count = ($count1 * 10) + $count2 ;  
  15.           $text = "" ;  
  16.           for($i=1;$i<6;$i++){  
  17.             $text .=  substr($this->cd_barras[$count1],($i-1),1) . substr($this->cd_barras[$count2],($i-1),1);  
  18.           }  
  19.           $this->cd_barras[$count] = $text;  
  20.        }  
  21.       }  
  22.      
  23.           //$img         = imagecreate($lower*95+300,$hight+30); 
  24.           $img         = imagecreate(145,55);  
  25.      
  26.     //$img         = imagecreate(395,73);    
  27.           $cl_black = imagecolorallocate($img, 0, 0, 0);  
  28.           $cl_white = imagecolorallocate($img, 255, 255, 255);  
  29.      
  30.                 
  31.             
  32.           imagefilledrectangle($img, 0, 0, $lower*95+1000, $hight+30, $cl_white);  
  33.             
  34.      
  35.           imagefilledrectangle($img, 1,1,1,53,$cl_black);  
  36.           imagefilledrectangle($img, 2,1,2,53,$cl_white);  
  37.           imagefilledrectangle($img, 3,1,3,53,$cl_black);  
  38.           imagefilledrectangle($img, 4,1,4,53,$cl_white);  
  39.      
  40.      
  41.      
  42.     $thin = 1 ;  
  43.     if(substr_count(strtoupper($_server['server_software']),"win32")){ 
  44.         //o tamanho para windows tem que ser 3 
  45.         // for windows, the wide bar has = 3 
  46.          $wide = 3; 
  47.     } else { 
  48.             $wide = 2.72; 
  49.        } 
  50.     $pos   = 5 ;  
  51.     $text = $value ;  
  52.     if((strlen($text) % 2) <> 0){  
  53.         $text = "0" . $text;  
  54.     }  
  55.      
  56.      
  57.     while (strlen($text) > 0) {  
  58.       $i = round($this->barra_left($text,2));  
  59.       $text = $this->barra_right($text,strlen($text)-2);  
  60.         
  61.       $f = $this->cd_barras[$i];  
  62.         
  63.       for($i=1;$i<11;$i+=2){  
  64.         if (substr($f,($i-1),1) == "0") {  
  65.           $f1 = $thin ;  
  66.         }else{  
  67.           $f1 = $wide ;  
  68.         }  
  69.      
  70.         
  71.       imagefilledrectangle($img, $pos,1,$pos-1+$f1,53,$cl_black)  ;  
  72.       $pos = $pos + $f1 ;    
  73.         
  74.       if (substr($f,$i,1) == "0") {  
  75.           $f2 = $thin ;  
  76.         }else{  
  77.           $f2 = $wide ;  
  78.         }  
  79.      
  80.       imagefilledrectangle($img, $pos,1,$pos-1+$f2,53,$cl_white)  ;  
  81.       $pos = $pos + $f2 ;    
  82.       }  
  83.     }  
  84.      
  85.          
  86.     imagefilledrectangle($img, $pos,1,$pos-1+$wide,53,$cl_black);  
  87.     $pos=$pos+$wide;  
  88.      
  89.     imagefilledrectangle($img, $pos,1,$pos-1+$thin,53,$cl_white);  
  90.     $pos=$pos+$thin;  
  91.      
  92.      
  93.     imagefilledrectangle($img, $pos,1,$pos-1+$thin,53,$cl_black);  
  94.     $pos=$pos+$thin;  
  95.      
  96.     $this->put_img($img,$files); 
  97.     }  
  98.      
  99.     function barra_left($input,$comp){  
  100.         return substr($input,0,$comp);  
  101.     }  
  102.      
  103.     function barra_right($input,$comp){  
  104.         return substr($input,strlen($input)-$comp,$comp);  
  105.     }  
  106.      
  107.     function put_img($image,$file){ 
  108.         if($this->into){             
  109.    imagegif($image,$file);  
  110.         }  
  111.   else {//开源代码Vevb.com 
  112.                     header("content-type: image/gif"); 
  113.                     imagegif($image); 
  114.              } 
  115.         imagedestroy($image); 
  116.     } 
  117. } 

调用方法,代码如下:

  1. <?php 
  2.   include("codes.php"); 
  3.  $new_code = new cd_barra("1234567890","a.gif",1); 
  4.  ?> 
  5.  <img src="a.gif"   /> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表