function draw_upc_barcode ($number, $show_numbers
{
$number = str_replacearray'-'' '), '', $number);
if (strlen$number)!=12)
throw new Exception"UPC number must have 12 digits");
for ($i=0;$i<12;$i++) {
if (!is_numeric$number$i]))
throw new Exception"UPC number must contain only digits");
}
$lcodes = array
'0001101'
'0011001'
'0010011'
'0111101'
'0100011'
'0110001'
'0101111'
'0111011'
'0110111'
'0001011'
);
$rcodes = array
'1110010'
'1100110'
'1101100'
'1000010'
'1011100'
'1001110'
'1010000'
'1000100'
'1001000'
'1110100'
);
$code = '101'
for ($i=0;$i<6;$i++) {
$code .= $lcodes$number$i]];
}
$code .= '01010'
for ($i=6;$i<12;$i++) {
$code .= $rcodes$number$i]];
}
$code .= '101'
// create image
$width=190;
$height=100;
$image = image_create_alpha$width, $height);
$white = imagecolorallocate$image, 255, 255, 255);
imagefilledrectangle$image, 0, 0, $width, $height, $white);
// draw lines
$black = imagecolorallocate$image, 0, 0, 0);
for ($i=0;$istrlen$code);$i++) {
if ($code$i]=='1') {
imageline$image, $i*2,0, $i*2, $height, $black);
imageline$image, $i*2+1,0, $i*2+1, $height, $black);
}
}
// draw numbers
if ($show_numbers) {
imagefilledrectangle$image, 6, $height-16, 90, $height, $white);
imagefilledrectangle$image, 98, $height-16, 182, $height, $white);
for ($i=0;$i<6;$i++) {
imagestring$image, 2, 11+$i*14, $height-14, $number$i], $black);
}
for ($i=6;$i<12;$i++) {
imagestring$image, 2, 19+$i*14, $height-14, $number$i], $black);
}
}
return $image
}