Php QR code generation

Source: Internet
Author: User
With the development of technology, QR codes are everywhere, especially in the era of smart phones. the following section describes how to use php to generate QR codes, for more information, see bar codes and QR codes. these codes are more active in the smart phone era, today, I want to share with you how to use PHP to generate a QR code. As for how to generate a QR code, we usually use google's open api to generate a QR code. let's get down to the truth and go directly to the code:

Method 1:

Use Google API to generate QR code

Google provides a comprehensive interface for generating QR codes. the following code calls an API:

$ UrlToEncode = "http://www.bitsCN.com"; generateQRfromGoogle ($ urlToEncode);/*** google api QR code generation [QRcode can store up to 4296 letters and numbers of any text, for details, see the QR code data format. * @ param string $ information contained in the two-dimensional code, which can be numbers, characters, binary information, and Chinese characters. Cannot mix data type, data must go through the UTF-8 URL-encoded * @ param int $ widhtHeight to generate the dimensional settings of the QR code * @ param string $ EC_level optional error correction level, QR code supports four levels of error correction, used to restore lost, read-wrong, fuzzy, and data. * L-default: 7% of lost data can be identified * M-15% of lost data can be identified * Q-25% of lost data can be identified * H-30% of lost data can be identified *@ param int $ the distance between the QR code generated by margin and the image border */function generateQRfromGoogle ($ chlorophyll, $ widhtHeight = '000000', $ EC_level = 'l', $ margin = '0') {$ chlorophyll = urlencode ($ chlorophyll); echo '';}

Use php qr Code to generate a class library php qr Code to generate a QR Code

Php qr CodeIt is a php qr code generation class library that can be used to easily generate QR codes. the official website provides download and multiple demo demos at: http://phpqrcode.sourceforge.net /.
After downloading the class library provided by the official website, you only need to usePhpqrcode. phpYou can generate a QR code. of course, GD2 must be enabled in your PHP environment. Phpqrcode. php provides a key png () method. the parameter $ text indicates that two pieces of information are generated. the parameter $ outfile indicates whether to output the two-dimensional code image file. the default value is No; the $ level parameter indicates the coverage rate, that is, the covered areas can also be identified, namely, L (QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25% ), H (QR_ECLEVEL_H, 30%); parameter $ size indicates the size of the generated image; default value: 3; parameter $ margin indicates the gap between blank areas of the border around the QR code; the parameter $ saveandprint indicates whether to save the QR code and display it.

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); } 

Calling php qr Code is very simple, the following Code can generate a content for "http://www.bitsCN.com" QR Code.

include 'phpqrcode.php'; QRcode::png('http://www.bitsCN.com'); 

In practice, we will add our own LOGO in the middle of the QR code to enhance the promotion effect. How to generate a QR code containing a logo? In fact, the principle is very simple. First, use the php qr Code to generate a QR Code image, and then use the php image function to add the prepared logo image to the middle of the generated original QR Code image, then generate a new QR code image.

Include 'phpqrcode. php '; $ value = 'http: // www.bitsCN.com'; // QR code content $ errorCorrectionLevel = 'l'; // fault tolerance level $ matrixPointSize = 6; // Generate image size // Generate QR code image QRcode: png ($ value, 'qrcode.png ', $ errorCorrectionLevel, $ matrixPointSize, 2); $ logo = 'logo.png '; // The prepared logo image $ QR = 'qrcode.png '; // The generated original QR code image if ($ logo! = FALSE) {$ QR = imagecreatefromstring (file_get_contents ($ QR); $ logo = imagecreatefromstring (file_get_contents ($ logo); $ QR_width = imagesx ($ QR ); // QR code image width $ QR_height = imagesy ($ QR); // QR code image height $ logo_width = imagesx ($ logo ); // logo image width $ logo_height = imagesy ($ logo); // logo image 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; // re-combine the image and resize imagecopyresampled ($ QR, $ logo, $ from_width, $ from_width, 0, 0, $ logo_qr_width, $ logo_qr_height, $ logo_width, $ logo_height);} // output image imagepng ($ QR, 'jb51.png '); echo '';

Method 2:

No nonsense. go directly to the code:

<?php function UPCAbarcode($code) {  $lw = 2; $hi = 100;  $Lencode = array('0001101','0011001','0010011','0111101','0100011',    '0110001','0101111','0111011','0110111','0001011');  $Rencode = array('1110010','1100110','1101100','1000010','1011100',    '1001110','1010000','1000100','1001000','1110100');  $ends = '101'; $center = '01010';  /* 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
 
  =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);  }  /* Add the Human Readable Label */  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. */  header("Content-Type: image/png");  ImagePNG($img); } ?>
 

Because the QR code allows some fault tolerance, even if the hidden part of the QR code is still decoded, the scan results can be decoded when we scan the QR code in less than half, this is because the generator repeatedly expresses some information to improve the fault tolerance. This is why we add a LOGO image in the middle of the QR code without affecting the decoding result.

The above two methods are used to introduce php to generate a QR code, hoping to help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.