1 | <?php |
||
2 | |||
3 | /* |
||
4 | * Author David S. Tufts |
||
5 | * Company davidscotttufts.com |
||
6 | * |
||
7 | * Date: 05/25/2003 |
||
8 | * Usage: <img src="/barcode.php?text=testing" alt="testing" /> |
||
9 | */ |
||
10 | |||
11 | // For demonstration purposes, get pararameters that are passed in through $_GET or set to the default value |
||
12 | $filepath = (isset($_GET['filepath']) ? $_GET['filepath'] : ''); |
||
13 | $text = (isset($_GET['text']) ? $_GET['text'] : '0'); |
||
14 | $size = (isset($_GET['size']) ? $_GET['size'] : '20'); |
||
15 | $orientation = (isset($_GET['orientation']) ? $_GET['orientation'] : 'horizontal'); |
||
16 | $code_type = (isset($_GET['codetype']) ? $_GET['codetype'] : 'code128'); |
||
17 | $print = (isset($_GET['print']) && $_GET['print'] == 'true' ? true : false); |
||
18 | $sizefactor = (isset($_GET['sizefactor']) ? $_GET['sizefactor'] : '1'); |
||
19 | |||
20 | // This function call can be copied into your project and can be made from anywhere in your code |
||
21 | barcode($filepath, $text, $size, $orientation, $code_type, $print, $sizefactor); |
||
22 | |||
23 | function barcode($filepath = '', $text = '0', $size = '20', $orientation = 'horizontal', $code_type = 'code128', $print = false, $SizeFactor = 1) |
||
24 | { |
||
25 | $code_string = ''; |
||
26 | // Translate the $text into barcode the correct $code_type |
||
27 | if (in_array(strtolower($code_type), ['code128', 'code128b'])) { |
||
28 | $chksum = 104; |
||
29 | // Must not change order of array elements as the checksum depends on the array's key to validate final code |
||
30 | $code_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']; |
||
31 | $code_keys = array_keys($code_array); |
||
32 | $code_values = array_flip($code_keys); |
||
33 | for ($X = 1; $X <= strlen($text); $X++) { |
||
34 | $activeKey = substr($text, ($X - 1), 1); |
||
35 | $code_string .= $code_array[$activeKey]; |
||
36 | $chksum = ($chksum + ($code_values[$activeKey] * $X)); |
||
37 | } |
||
38 | $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]]; |
||
39 | |||
40 | $code_string = '211214'.$code_string.'2331112'; |
||
41 | } elseif (strtolower($code_type) == 'code128a') { |
||
42 | $chksum = 103; |
||
43 | $text = strtoupper($text); // Code 128A doesn't support lower case |
||
44 | // Must not change order of array elements as the checksum depends on the array's key to validate final code |
||
45 | $code_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', 'NUL'=>'111422', 'SOH'=>'121124', 'STX'=>'121421', 'ETX'=>'141122', 'EOT'=>'141221', 'ENQ'=>'112214', 'ACK'=>'112412', 'BEL'=>'122114', 'BS'=>'122411', 'HT'=>'142112', 'LF'=>'142211', 'VT'=>'241211', 'FF'=>'221114', 'CR'=>'413111', 'SO'=>'241112', 'SI'=>'134111', 'DLE'=>'111242', 'DC1'=>'121142', 'DC2'=>'121241', 'DC3'=>'114212', 'DC4'=>'124112', 'NAK'=>'124211', 'SYN'=>'411212', 'ETB'=>'421112', 'CAN'=>'421211', 'EM'=>'212141', 'SUB'=>'214121', 'ESC'=>'412121', 'FS'=>'111143', 'GS'=>'111341', 'RS'=>'131141', 'US'=>'114113', 'FNC 3'=>'114311', 'FNC 2'=>'411113', 'SHIFT'=>'411311', 'CODE C'=>'113141', 'CODE B'=>'114131', 'FNC 4'=>'311141', 'FNC 1'=>'411131', 'Start A'=>'211412', 'Start B'=>'211214', 'Start C'=>'211232', 'Stop'=>'2331112']; |
||
46 | $code_keys = array_keys($code_array); |
||
47 | $code_values = array_flip($code_keys); |
||
48 | for ($X = 1; $X <= strlen($text); $X++) { |
||
49 | $activeKey = substr($text, ($X - 1), 1); |
||
50 | $code_string .= $code_array[$activeKey]; |
||
51 | $chksum = ($chksum + ($code_values[$activeKey] * $X)); |
||
52 | } |
||
53 | $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]]; |
||
54 | |||
55 | $code_string = '211412'.$code_string.'2331112'; |
||
56 | } elseif (strtolower($code_type) == 'code39') { |
||
57 | $code_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']; |
||
58 | |||
59 | // Convert to uppercase |
||
60 | $upper_text = strtoupper($text); |
||
61 | |||
62 | for ($X = 1; $X <= strlen($upper_text); $X++) { |
||
63 | $code_string .= $code_array[substr($upper_text, ($X - 1), 1)].'1'; |
||
64 | } |
||
65 | |||
66 | $code_string = '1211212111'.$code_string.'121121211'; |
||
67 | } elseif (strtolower($code_type) == 'code25') { |
||
68 | $code_array1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']; |
||
69 | $code_array2 = ['3-1-1-1-3', '1-3-1-1-3', '3-3-1-1-1', '1-1-3-1-3', '3-1-3-1-1', '1-3-3-1-1', '1-1-1-3-3', '3-1-1-3-1', '1-3-1-3-1', '1-1-3-3-1']; |
||
70 | |||
71 | for ($X = 1; $X <= strlen($text); $X++) { |
||
72 | for ($Y = 0; $Y < count($code_array1); $Y++) { |
||
0 ignored issues
–
show
|
|||
73 | if (substr($text, ($X - 1), 1) == $code_array1[$Y]) { |
||
74 | $temp[$X] = $code_array2[$Y]; |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | |||
79 | for ($X = 1; $X <= strlen($text); $X += 2) { |
||
80 | if (isset($temp[$X]) && isset($temp[($X + 1)])) { |
||
81 | $temp1 = explode('-', $temp[$X]); |
||
82 | $temp2 = explode('-', $temp[($X + 1)]); |
||
83 | for ($Y = 0; $Y < count($temp1); $Y++) { |
||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||
84 | $code_string .= $temp1[$Y].$temp2[$Y]; |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | $code_string = '1111'.$code_string.'311'; |
||
90 | } elseif (strtolower($code_type) == 'codabar') { |
||
91 | $code_array1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '$', ':', '/', '.', '+', 'A', 'B', 'C', 'D']; |
||
92 | $code_array2 = ['1111221', '1112112', '2211111', '1121121', '2111121', '1211112', '1211211', '1221111', '2112111', '1111122', '1112211', '1122111', '2111212', '2121112', '2121211', '1121212', '1122121', '1212112', '1112122', '1112221']; |
||
93 | |||
94 | // Convert to uppercase |
||
95 | $upper_text = strtoupper($text); |
||
96 | |||
97 | for ($X = 1; $X <= strlen($upper_text); $X++) { |
||
98 | for ($Y = 0; $Y < count($code_array1); $Y++) { |
||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||
99 | if (substr($upper_text, ($X - 1), 1) == $code_array1[$Y]) { |
||
100 | $code_string .= $code_array2[$Y].'1'; |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | $code_string = '11221211'.$code_string.'1122121'; |
||
105 | } |
||
106 | |||
107 | // Pad the edges of the barcode |
||
108 | $code_length = 20; |
||
109 | if ($print) { |
||
110 | $text_height = 30; |
||
111 | } else { |
||
112 | $text_height = 0; |
||
113 | } |
||
114 | |||
115 | for ($i = 1; $i <= strlen($code_string); $i++) { |
||
116 | $code_length = $code_length + (int) (substr($code_string, ($i - 1), 1)); |
||
117 | } |
||
118 | |||
119 | if (strtolower($orientation) == 'horizontal') { |
||
120 | $img_width = $code_length * $SizeFactor; |
||
121 | $img_height = $size; |
||
122 | } else { |
||
123 | $img_width = $size; |
||
124 | $img_height = $code_length * $SizeFactor; |
||
125 | } |
||
126 | |||
127 | $image = imagecreate($img_width, $img_height + $text_height); |
||
128 | $black = imagecolorallocate($image, 0, 0, 0); |
||
129 | $white = imagecolorallocate($image, 255, 255, 255); |
||
130 | |||
131 | imagefill($image, 0, 0, $white); |
||
132 | if ($print) { |
||
133 | imagestring($image, 5, 31, $img_height, $text, $black); |
||
134 | } |
||
135 | |||
136 | $location = 10; |
||
137 | for ($position = 1; $position <= strlen($code_string); $position++) { |
||
138 | $cur_size = $location + (substr($code_string, ($position - 1), 1)); |
||
139 | if (strtolower($orientation) == 'horizontal') { |
||
140 | imagefilledrectangle($image, $location * $SizeFactor, 0, $cur_size * $SizeFactor, $img_height, ($position % 2 == 0 ? $white : $black)); |
||
141 | } else { |
||
142 | imagefilledrectangle($image, 0, $location * $SizeFactor, $img_width, $cur_size * $SizeFactor, ($position % 2 == 0 ? $white : $black)); |
||
143 | } |
||
144 | $location = $cur_size; |
||
145 | } |
||
146 | |||
147 | // Draw barcode to the screen or save in a file |
||
148 | if ($filepath == '') { |
||
149 | header('Content-type: image/png'); |
||
150 | imagepng($image); |
||
151 | imagedestroy($image); |
||
152 | } else { |
||
153 | imagepng($image, $filepath); |
||
154 | imagedestroy($image); |
||
155 | } |
||
156 | } |
||
157 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: