Use PHP to generate a QR code

Source: Internet
Author: User

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;

?>

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.