# Php generates a barcode

Source: Internet
Author: User
Tags strlen

Php generates a barcode
<? Php
Function UPCAbarcode (\$ code ){
\$ Lw = 2; \$ hi = 100;
\$ Lencode = array ('20140901', '20160901', '20160901', '20160301', '20160301 ',
'123', '123', '123', '123', '123 ');
\$ Rencode = array ('20140901', '20160901', '20160901', '20160301', '20160301 ',
'123', '123', '123', '123', '123 ');
\$ Ends = '20180101'; \$ center = '20180101 ';
/* UPC-A Must be 11 digits, we compute the checksum .*/
If (strlen (\$ code )! = 11) {die ("UPC-A Must be 11 digits .");}
/* Compute the EAN-13 Checksum digit */
\$ Ncode = '0'. \$ code;
\$ Even = 0; \$ odd = 0;
For (\$ x = 0; \$ x <12; \$ x ++ ){
If (\$ x % 2) {\$ odd + = \$ ncode [\$ x];} else {\$ even + = \$ ncode [\$ x];}
}
\$ Code. = (10-(\$ odd * 3 + \$ even) % 10) % 10;
/* Create the bar encoding using a binary string */
\$ Bars = \$ ends;
\$ Bars. = \$ Lencode [\$ code [0];
For (\$ x = 1; \$ x <6; \$ x ++ ){
\$ Bars. = \$ Lencode [\$ code [\$ x];
}
\$ Bars. = \$ center;
For (\$ x = 6; \$ x <12; \$ x ++ ){
\$ Bars. = \$ Rencode [\$ code [\$ x];
}
\$ Bars. = \$ ends;
/* Generate the Barcode Image */
\$ Img = ImageCreate (\$ lw * 95 + 30, \$ hi + 30 );
\$ Fg = ImageColorAllocate (\$ img, 0, 0, 0 );
\$ Bg = ImageColorAllocate (\$ img, 255,255,255 );
ImageFilledRectangle (\$ img, 0, 0, \$ lw * 95 + 30, \$ hi + 30, \$ bg );
\$ Shift = 10;
For (\$ x = 0; \$ x <strlen (\$ bars); \$ x ++ ){
If (\$ x <10) | (\$ x >=45 & \$ x <50) | (\$ x >=85) {\$ sh = 10 ;} else {\$ sh = 0 ;}
If (\$ bars [\$ x] = '1') {\$ color = \$ fg;} else {\$ color = \$ bg ;}
ImageFilledRectangle (\$ img, (\$ x * \$ lw) + 15, 5, (\$ x + 1) * \$ lw + 14, \$ hi + 5 + \$ sh, \$ color );
}
ImageString (\$ img, 4,5, \$ hi-5, \$ code [0], \$ fg );
For (\$ x = 0; \$ x <5; \$ x ++ ){
ImageString (\$ img, 5, \$ lw * (13 + \$ x * 6) + 15, \$ hi + 5, \$ code [\$ x + 1], \$ fg );
ImageString (\$ img, 5, \$ lw * (53 + \$ x * 6) + 15, \$ hi + 5, \$ code [\$ x + 6], \$ fg );
}
ImageString (\$ img, 4, \$ lw * 95 + 17, \$ hi-5, \$ code [11], \$ fg );
/* Output the Header and Content .*/
ImagePNG (\$ img );
}
UPCAbarcode ('20140901 ');
?>

Related Keywords:
Related Article

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD