1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
| /*
* PHP Barcode
* Written by Jeremy Curcio
* This work is licensed under a Creative Commons
* Attribution-ShareAlike 3.0 Unported License.
*/
function upcaBarcode($numbers, $width, $height) {
//Set output to be an image.
header("Content-type:image/png");
//Create image object and colors and define width of bars.
if ($width < 190) $width = 190;
$barWidth = $width/95;
$img=imagecreate($width, $height);
$black_color=imagecolorallocate($img,0,0,0);
$white_color=imagecolorallocate($img,255,255,255);
//Set the background to white.
imagefilledrectangle($img, 0, 0, 200, 60, $white_color);
//Check to make sure $numbers is 12 characters long.
$check = (intval(substr($numbers,0,1))*3)+intval(substr($numbers,1,1))+(intval(substr($numbers,2,1))*3)+intval(substr($numbers,3,1))+(intval(substr($numbers,4,1))*3)+intval(substr($numbers,5,1))+(intval(substr($numbers,6,1))*3)+intval(substr($numbers,7,1))+(intval(substr($numbers,8,1))*3)+intval(substr($numbers,9,1))+(intval(substr($numbers,10,1))*3);
if ($check != 0) $check = 10-substr($check,strlen($check)-1,1);
$numbers = $numbers.$check;
$c = $numbers;
if ((strlen($numbers) == 12) && (is_numeric($numbers))) {
//Append start and finish characters to the barcode.
$numbers = "z".$numbers."z";
//Find the middle of the string, break the string in half
//then add the middle separater to the barcode.
$half = (int) ( (strlen($numbers) / 2) );
$left = substr($numbers, 0, $half);
$right = substr($numbers, $half);
$middle = "sbsbs";
$numbers = $left.$middle;
$numbers2 = $right;
//Replace the left half's numbers with the correct pattern.
$numbers = str_replace("z","sbsbs",$numbers);
$numbers = str_replace("0","sssbbsb",$numbers);
$numbers = str_replace("1","ssbbssb",$numbers);
$numbers = str_replace("2","ssbssbb",$numbers);
$numbers = str_replace("3","sbbbbsb",$numbers);
$numbers = str_replace("4","sbsssbb",$numbers);
$numbers = str_replace("5","sbbsssb",$numbers);
$numbers = str_replace("6","sbsbbbb",$numbers);
$numbers = str_replace("7","sbbbsbb",$numbers);
$numbers = str_replace("8","sbbsbbb",$numbers);
$numbers = str_replace("9","sssbsbb",$numbers);
//Replace the right half's numbers with the correct pattern.
$numbers2 = str_replace("0","bbbssbs",$numbers2);
$numbers2 = str_replace("1","bbssbbs",$numbers2);
$numbers2 = str_replace("2","bbsbbss",$numbers2);
$numbers2 = str_replace("3","bssssbs",$numbers2);
$numbers2 = str_replace("4","bsbbbss",$numbers2);
$numbers2 = str_replace("5","bssbbbs",$numbers2);
$numbers2 = str_replace("6","bsbssss",$numbers2);
$numbers2 = str_replace("7","bsssbss",$numbers2);
$numbers2 = str_replace("8","bssbsss",$numbers2);
$numbers2 = str_replace("9","bbbsbss",$numbers2);
$numbers2 = str_replace("z","sbsbs",$numbers2);
//Create one string will all of the encodings.
$finalEncoding = $numbers.$numbers2;
//Add the bars and spaces to the image.
for ($i = 0; $i < strlen($finalEncoding); $i++) {
if (strcmp($finalEncoding[$i],"b") == 0) imagefilledrectangle($img, $i*$barWidth, 0, ($i*$barWidth)+$barWidth, $height, $black_color);
else imagefilledrectangle($img, $i*$barWidth, 0, ($i*$barWidth)+$barWidth, $height, $white_color);
}
}
//If $numbers isn't 12 characters wide produce error image.
else {
imagestring($img, 4, 0, 12, "Error: Input must be 12", $black_color);
imagestring($img, 4, 0, 24, "characters (0-9) long.", $black_color);
}
//Return completed image.;
return (imagepng($img));
} |