php生成条形码代码

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

php生成条形码代码如下:

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

调用方法,代码如下:

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