Today, with extensive application of QR codes, it is the most basic requirement to automatically generate corresponding QR codes on websites. This article introduces three ways to automatically generate a QR code using PHP.
Method 1 of the get method:
$ UrlToEncode = "163.com ";
GenerateQRfromGoogle ($ urlToEncode );
Function generateQRfromGoogle ($ chlorophyll, $ widhtHeight = '000000', $ EC_level = 'l', $ margin = '0 '){
$ Url = urlencode ($ url );
Return'Chld = '. $ EC_level. '| '. $ margin. '& chlorophyll = '. $ chlorophyll. '"alt =" QR code "widhtHeight = "'. $ size. '"widhtHeight = "'. $ size. '"/> ';
}
Post method implementation:
$ Width = 300;
$ Height = 300;
$ String = "163.com ";
Function qrcode ($ width, $ height, $ string)
{
$ Post_data = array ();
$ Post_data ['cht'] = 'qr ';
$ Post_data ['chs '] = $ width. "x". $ height;
$ Post_data ['check'] = $ 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 );
// Echo $ data;
$ 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 "
Return $ result;
}
Header ("Content-type: image/png ");
Echo qrcode ($ width, $ height, $ string );
2. php QR Code library is used to implement
First download the class library package
Address: http://phpqrcode.sourceforge.net/
Download: http://sourceforge.net/projects/phpqrcode/
API documentation
Detailed examples
<?
Include "./phpqrcode. php ";
$ Value = "http://www.weste.net ";
$ ErrorCorrectionLevel = "L ";
$ MatrixPointSize = "4 ";
QRcode: png ($ value, false, $ errorCorrectionLevel, $ matrixPointSize );
Exit;
?>