PHP generates two-dimensional code _php instance

Source: Internet
Author: User
With the progress of science and technology, we often see barcodes and QR codes, especially in the era of smart phones are more active, today I give you to share how to use PHP to generate two-dimensional code, as to how to generate two-dimensional code, commonly used in the test call Google's Open API to generate, to the bottom, directly on the code:

The first method:

Using the Google API to generate two-dimensional code

Google provides a more complete QR code generation interface, call API interface is very simple, the following is the calling code:

$urlToEncode = "Http://www.php.net"; Generateqrfromgoogle ($urlToEncode); /**  * Google API qr code generation "QRCode can store up to 4,296 alphanumeric types of arbitrary text, you can view the QR code data format"  * @param string $chl QR Code contains information, can be numbers, characters, Binary information, Chinese characters.  data types cannot be mixed, data must go through UTF-8 url-encoded  * @param int $widhtHeight dimension setting for QR code  * @param string $EC _level Optional error correction level, QR code supports four levels of error correction to recover lost, misread, blurred, and data.  *    L-Default: can identify loss of 7% of data  *    M can identify loss of 15% of data  *    Q can identify lost 25% of the data  *    H can identify lost 30% of the data  * @ param int $margin The distance generated by the two-dimensional code from the picture border  */function Generateqrfromgoogle ($chl, $widhtHeight = ' _level= ', $EC ' L ', $ margin= ' 0 ') {  $chl = UrlEncode ($CHL);  

Generate a class library using PHP QR code generate QR code

PHP QR code is a php two-dimensional code generation library, using it can easily generate two-dimensional code, the official website provides a download and demo demo, view address: http://phpqrcode.sourceforge.net/.
After downloading the class library provided by the official website, we only need to use phpqrcode.php to generate the QR code, of course, your PHP environment must turn on support GD2. Phpqrcode.php provides a key PNG () method, where the parameter $text represents the generation of two-bit information text, the parameter $outfile indicates whether to output a QR code picture file, the default is no, the parameter $level indicates fault tolerance, that is, the covered area can be recognized, respectively L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_h,30%), parameter $size indicates the image size is generated, default is 3; Margin indicates the spacing value of the border space around the QR code, and 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);  

Calling PHP QR code is very simple, and the following code generates a QR code that reads "Http://www.php.net".

In the actual application, we will add their own logo in the middle of the two-dimensional code, has enhanced the publicity effect. How do you generate a QR code that contains a logo? In fact, the principle is very simple, first use PHP QR code to generate a two-dimensional code image, and then the use of PHP image correlation function, the pre-prepared logo image is added to the newly generated image of the original two-dimensional code, and then regenerate a new QR code image.

include ' phpqrcode.php '; $value = ' http://www.php.net ';//two-dimensional code content $ Errorcorrectionlevel = ' L ';//fault tolerance level $matrixPointSize = 6;//Generate picture size//generate QR code picture qrcode::p ng ($value, ' qrcode.png ', $ Errorcorrectionlevel, $matrixPointSize, 2); $logo = ' logo.png ';///Ready logo image $QR = ' qrcode.png ';//The original two-dimensional code figure already generated 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 Picture width $logo _he  ight = Imagesy ($logo);//logo picture 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; Regroup and resize the picture imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $logo _qr_width, $logo _qr_height, $logo _wid Th, $logo _height); }//Output picture imagepng ($QR, ' php.png '); Echo '; 

Method Two:

No nonsense, directly on 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 one digits, we compute the checksum.  */if (strlen ($code)! =) {die ("upc-a must is one 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. = (($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
 
  <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);  }/* 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 [one], $FG); /* Output the Header and Content.  */Header ("Content-type:image/png"); Imagepng ($IMG); }?>
 

Because the two-dimensional code allows a certain degree of fault tolerance, the general QR code even in the masked part but still able to decode, often we scan the QR code when scanned to even less than half of the scanning results can be decoded, because the generator will be part of the information repeated representation to improve its fault tolerance, This is why we add a logo image in the middle of the QR code does not affect the decoding result of the reason.

The above through two methods to introduce PHP to generate two-dimensional code, I hope to help everyone.

  • Related Article

    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.