Using PHP to generate two-dimensional code _php tutorial

Source: Internet
Author: User

Introduction: In the two-dimensional code widely used today, in the Web site automatically generate the corresponding QR code is the most basic requirements. This article describes three ways to automatically generate two-dimensional code using PHP.




Get method Implementation method one:

$urlToEncode = "163.com";

Generateqrfromgoogle ($urlToEncode);

function Generateqrfromgoogle ($chl, $widhtHeight = ' Max ', $EC _level= ' L ', $margin = ' 0 ') {

$url = UrlEncode ($url );

return ' chld= '. $EC _level. ' | '. $margin. ' &chl= '. $chl. ' "alt=" QR Code "widhtheight=" '. $size. ' widhtheight= ' '. $size. ' /> ';

}

How to implement the Post method:

$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[' 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 );

//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 ""; Note that you do not write the header

return $result;

}

Header ("Content-type:image/png");

Echo QRCode ($width, $height, $string);

2. Use PHP library PHP QR code to achieve

First download the Class library package

Address: http://phpqrcode.sourceforge.net/

Download: http://sourceforge.net/projects/phpqrcode/

    • API documentation

    • A detailed example

Include "./phpqrcode/phpqrcode.php";

$value = "Http://www.weste.net";

$errorCorrectionLevel = "L";

$matrixPointSize = "4";

QRCode::p ng ($value, False, $errorCorrectionLevel, $matrixPointSize);

Exit

?>

http://www.bkjia.com/PHPjc/735051.html www.bkjia.com true http://www.bkjia.com/PHPjc/735051.html techarticle Introduction : In the two-dimensional code widely used today, in the Web site automatically generate the corresponding QR code is the most basic requirements. This article describes three ways to automatically generate two-dimensional code using PHP. ...

  • 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.