通過 PHP 產生 一維碼
此代碼來自 http://www.nixiaofeng.com/110.html
13) die('條碼長度不正確'); if (strlen($code) == 12) { // 計算校正位 $lsum = 0; $rsum = 0; for($i=1; $i<=strlen($code); $i++) { if($i % 2) { $lsum += (int)$code[$i-1]; }else{ $rsum += (int)$code[$i-1]; } } $tsum = $lsum + $rsum * 3; $chkdig = 10 - ($tsum % 10); if ($chkdig == 10) $chkdig = 0; $code .= $chkdig; } // 定義起始付 $start = '101'; // 定義中止符 $end = '101'; // 定義中間分隔字元 $center = '01010'; // 定義左資料碼 $Guide = array(0=>'AAAAAA','AABABB','AABBAB','AABBBA','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA'); // 定義左側碼,分為“A”、“B”兩種 $Lencode = array("A" => array('0001101','0011001','0010011','0111101','0100011','0110001','0101111','0111011','0110111','0001011'), "B" => array('0100111','0110011','0011011','0100001','0011101','0111001','0000101','0010001','0001001','0010111')); // 定義右側碼,統一為“C”編碼 $Rencode = array('1110010','1100110','1101100','1000010','1011100','1001110','1010000','1000100','1001000','1110100'); // 編碼起始符 $barcode = $start; // 編碼左資料位 for($i=1; $i<=6; $i++) { $barcode .= $Lencode[$Guide[$code[0]][($i-1)]][$code[$i]]; } // 編碼中間分隔字元 $barcode .= $center; // 編碼右資料位 for($i=7; $i<13; $i++) { $barcode .= $Rencode[$code[($i)]]; } // 編碼中止符 $barcode .= $end; // 定義每個條碼單元的寬度和高度,單位是像素 $width = 2; $height = 40; // 定義起始符、中止符、中間分隔字元的高度增量 $increment = 10; // 建立方形(×95是因為整個條碼共95個單元,+60和+30是給條碼圖片周圍留空白邊框) $img = ImageCreate($width*95+60,$height+30); // 目前這個方形是透明的 // “1”的顏色(黑)與“0”的顏色(白) $fg = ImageColorAllocate($img, 0, 0, 0); $bg = ImageColorAllocate($img, 255, 255, 255); // 以“0”的顏色(白色),填充整個方形 ImageFilledRectangle($img, 0, 0, $width*95+60, $height+30, $bg); // 迴圈編碼後的每一個單元,輸出條碼圖形 for ($x=0; $x=92)為中止符,($x>=45 && $x<50)為中間分隔字元 // 這3個需要將高度增加 if (($x<4) || ($x>=45 && $x<50) || ($x>=92)) { $increment = 10; } else { $increment = 0; } // 當編碼值為“1”時,輸出黑色;當編碼值為“0”時,輸出白色 if ($barcode[$x] == '1') { $color = $fg; } else { $color = $bg; } ImageFilledRectangle($img, ($x*$width)+30,5,($x+1)*$width+29,$height+$increment,$color); } ImageString($img, 5, 20, $height+5, $code[0], $fg); for ($x=0; $x<6; $x++) { // 左側識別碼 ImageString($img, 5, $width*(8+$x*6)+30, $height+5, $code[$x+1], $fg); // 右側識別碼 ImageString($img, 5, $width*(53+$x*6)+30, $height+5, $code[$x+7], $fg); } header("Content-Type: image/jpeg"); ImageJPEG($img, "", 100);}?>