This article describes two ways to generate two-dimensional code using PHP.
(1) Using Google to generate two-dimensional code of the Open Interface, the code is as follows:
/**
* Google API two-dimensional code generation "QRCode can store up to 4,296 alphanumeric types of any text, you can view two-dimensional code data format"
* @param string $data Two-dimensional code contains information, can be numbers, characters, Binary information, Chinese characters. Cannot mix data type, data must pass through UTF-8 url-encoded. If you need to pass more than 2K bytes of information, use the Post method
* @param int $widhtHeight Generate a two-dimensional code size setting
* @param String $EC _level Optional error correction level, QR code supports four levels of error correction, used to restore lost, read-wrong, fuzzy, data.
* L Default: Can identify lost 7% of data
* M can identify lost 15% of data *
Q can identify lost 25% of data
* H can identify lost 30% of data
* param int $margin generated two-dimensional code away from the picture border
/function Generateqrfromgoogle ($data, $widhtHeight = ' The ', $EC _level= ' L ' , $margin = ' 0 ') {
$url =urlencode ($data);
Echo ' ';
}
How to use:
$data = ' All rights reserved: Http://www.jb51.net ';
The Post method implementation requests that the Google API generate two-dimensional code:
function QRCode ($width, $height, $string) {
$post _data=array ();
$post _data[' cht ']= ' QR ';
$post _data[' CHS ']= $width. " X ". $height;
$post _data[' CHL ']= $string;
$post _data[' Choe ']= "UTF-8";
$url = "Http://chart.apis.google.com/chart";
$data _array=array ();
foreach ($post _data as $key => $value) {
$data _array[]= $key. ' = '. $value;
}
$data =implode ("&", $data _array);
$ch =curl_init ();
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_postfields, $data);
curl_setopt ($ch, Curlopt_returntransfer, 1);
$result =curl_exec ($ch);
echo "
How to use:
Header ("Content-type:image/png");
$width =300;
$height =300;
$data = ' All rights reserved: Http://www.jb51.net ';
Echo QRCode ($width, $height, $data);
Of course, the resulting picture is the same as the above.
(2) Using PHP QR code class library to generate two-dimensional code
Note Using this class library must first download the class library package and download the address:
Address: http://phpqrcode.sourceforge.net/
There are many examples of downloaded packages, which can be studied by themselves, and a simple use case is given below (the exact meaning of the parameters is similar to the above):
<?php
include "./phpqrcode.php";
$data = ' All rights reserved: Http://www.jb51.net ';
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRCode::p ng ($data, False, $errorCorrectionLevel, $matrixPointSize);
The above mentioned is the entire content of this article, I hope to be proficient in the production of PHP two-dimensional code can help.