Use Google API to generate two-dimensional code
Google offers a more comprehensive two-dimensional code generation interface, call the API interface is very simple, the following is the calling code:
$urlToEncode="http://www.goocode.net";
generateQRfromGoogle($urlToEncode);
/**
* Google api [QRcode two-dimensional code generation can store up to 4296 alphanumeric type any text, you can view a specific two-dimensional code data format]
*param String $ chl information contained in the two-dimensional code, can be numbers, characters, binary information, character.
You can not mix data types, data must be UTF-8 URL-encoded
*param Int $ widhtHeight generate two-dimensional code size settings
*param String $ EC_level optional error correction level, QR code support four levels of error correction is used to recover lost, misread, fuzzy data.
* L- default: You can identify the loss of 7% of the data
* M- can identify the 15% loss of data
* Q- can identify the loss of 25% of the data
* H- can identify the loss of 30% of the data
*param Int $ margin generated two-dimensional code from the picture frame distance
*/
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0')
{
$chl = urlencode($chl);
echo 'http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'
&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$widhtHeight.'
" widhtHeight="'.$widhtHeight.'"/>';
}
Two-dimensional code generation using PHP library to generate two-dimensional code PHP QR Code
PHP QR Code is a two-dimensional code generated PHP class library, using it can easily generate a two-dimensional code, the official website offers downloads and multiple presentations demo, see the address: http: //phpqrcode.sourceforge.net/.
After downloading the official website provides libraries, just use phpqrcode.php can generate two-dimensional code, of course, open your PHP environment must support GD2. phpqrcode.php provides a critical png () method, where the parameter $ text representation of the information generated two text; $ outfile parameter indicates whether the output of two-dimensional code image file, the default No; parameter $ level, said fault rate, which is there are also identifies areas are covered, namely L (QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25%), H (QR_ECLEVEL_H, 30%); parameter $ size represents generated image size The default is 3; Parameter $ margin represents the border around a blank area of the two-dimensional code spacing value; parameter $ saveandprint indicates whether to save the two-dimensional code and displayed.
public static function png($text, $outfile=false, $level=QR_ECLEVEL_L, $size=3, $margin=4,
$saveandprint=false)
{
$enc = QRencode::factory($level, $size, $margin);
return $enc->encodePNG($text, $outfile, $saveandprint=false);
}
Call PHP QR Code is very simple, the following code to generate a content "http://www.goocode.net" two-dimensional code.
include 'phpqrcode.php';
QRcode::png('http://www.goocode.net');
Then the actual application, we will add your own LOGO in the middle of a two-dimensional code has been enhanced publicity. How to generate a two-dimensional code that contains a logo it? In fact, the principle is very simple, first using PHP QR Code generating a two-dimensional code images, and then use php's image correlation function, will be prepared logo image is added to the original two-dimensional code just generated images in the middle, and then rebuild a new the two-dimensional code images.
include 'phpqrcode.php';
$value = 'http://www.goocode.net'; //content
$errorCorrectionLevel = 'L';//Fault-tolerance level
$matrixPointSize = 6;//image size
//Generate two-dimensional code images
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'logo.png';//Ready logo image
$QR = 'qrcode.png';//The original two-dimensional code has been generated in FIG
if ($logo !== FALSE) {
$QR = imagecreatefromstring(file_get_contents($QR));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);//width
$QR_height = imagesy($QR);//height
$logo_width = imagesx($logo);//logo width
$logo_height = imagesy($logo);//logo height
$logo_qr_width = $QR_width / 5;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//Reassemble the picture and resize
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
//export image
imagepng($QR, 'goocode.png');
echo '<img src="goocode.png">';
Since the two-dimensional code to allow a certain fault tolerance, the general two-dimensional code even if the cover part, but still be able to decode, we scanned the regular two-dimensional code can be decoded even when the scan time is less than half of the scan, this is because the Builder will be part of the information is repeated representation to improve its fault tolerance, which is why we add LOGO picture in the middle of a two-dimensional code does not affect the result of decoding reasons.