Two methods of creating QR code with logo image in PHP

Source: Internet
Author: User



This article mainly and everyone to share the PHP generation with logo image QR Code two methods, mainly in the form of text and code and share with you, hope to help everyone.






First, 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.cn";generateQRfromGoogle($urlToEncode);/** * google api QR code generation [QRcode can store any text of up to 4296 alphanumeric types, you can view 2D Code data format] * @param string $chl The information contained in the QR code can be numbers, characters, binary information, or Chinese characters. Cannot mix data types, data must pass UTF-8 URL-encoded * @param int $widhtHeight Generate QR code size setting * @param string $EC_level Optional error correction level, QR code supports four levels of error correction, used Recover lost, misread, obscure, and data. * L-default: can identify 7% of lost data * M - can identify 15% of lost data * Q - can identify 25% of lost data * H - can identify 30% of lost data * @param Int $margin The distance of the generated QR code from the border of the image */functiongenerateQRfromGoogle($chl,$widhtHeight='150',$EC_level='L',$margin='0'){ $chl= urlencode($chl) ; echo'.$widhtHeight.'x'.$widhtHeight.' &cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight=" '.$widhtHeight.' " widhtHeight="'.$widhtHeight.'"/>';}



Second, the use of PHP QR code to generate a class library PHP code to generate two-dimensional codes

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 in which the text of the parameter indicates that a two-bit message is generated, the text of the argument generates a two-bit message, and the parameter outfile indicates whether to output a QR code picture file, default no; That is, the covered area can also be identified, respectively L (qreclevell,7level means fault tolerance, that is, the covered area can also be identified, respectively L (qreclevell,7size to generate the image size, the default is 3 The parameter margin indicates the spacing value of the border space around the two-dimensional code, and the parameter margin indicates the spacing value of the border space around the two-dimensional code; The parameter saveandprint indicates whether to save the QR code and display it.



publicstaticfunctionpng($text,$outfile=false,$level=QR_ECLEVEL_L,$size=3,$margin=4,$saveandprint=false){ $enc= QRencode::factory($level,$size,$margin); return$enc->encodePNG($text,$outfile,$saveandprint=false);}




Calling PHP QR code is very simple, and the following code generates a QR code that reads "Http://www.jb51.net".
PHP code
Include ' phpqrcode.php ';
QRCode::p ng (' http://www.jb51.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.jb51.net'; //QR code content $errorCorrectionLevel='L';//Fault tolerance level $matrixPointSize= 6;//Generate image size / /Generate QR code::png($value,'qrcode.png',$errorCorrectionLevel,$matrixPointSize, 2);$logo='logo.png';//Prepared logo image $QR='qrcode .png';//The original QR code map that has been 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=imagey($QR);//QR code image height $logo_width=imagex($logo);//logo image width $logo_height=imagey($ Logo);//logo image 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; //re Combine images and resize imagecopyresampled($QR,$logo,$from_width,$from_width, 0, 0,$logo_qr_width, $logo_qr_height,$logo_width,$logo_height);}//output map imagepng ($ QR, 'helloweba.png'); echo '';


Here is the reference to the above code, do not produce image files, convenient to call, save the following code as img.php


<?phpinclude'phpqrcode.php';$value=$_GET['url'];//QR code content $errorCorrectionLevel='L';//Fault tolerance level $matrixPointSize= 6;//Generate image size // Generate QR code::png($value,'qrcode.png',$errorCorrectionLevel,$matrixPointSize, 2);$logo='jb51.png';//Ready logo image $QR='qrcode.png ';//The original QR code map that has been generated if($logo!== FALSE) { $QR= imagecreatefromstring(file_get_contents($QR)); $logo= imagecreatefromstring(file_get_contents($logo)); $QR_width=imagex ($QR);//QR code image width $QR_height=imagey($QR);//QR code image height $logo_width=imagex($logo);//logo image width $logo_height=imagey($logo) ;//logo image 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; //Recompose the image And resize imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);}//output image Head Er("Content-type: image/png");ImagePng($QR);


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.



Related recommendations:



PHP implementation with logo Two-dimensional code class



PHP generates two-dimensional code instances



PHP Two-dimensional code production and download method



The above is the PHP generation with logo image QR Code two methods of details, more attention to the PHP Chinese network other related articles!



Two methods of creating QR code with logo image in 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.