Home  >  Article  >  Backend Development  >  Generate QR code using PHP class library PHPqrCode

Generate QR code using PHP class library PHPqrCode

不言
不言Original
2018-07-07 14:02:033206browse

This article mainly introduces the use of PHP class library PHPqrCode to generate QR codes. It has certain reference value. Now I share it with you. Friends in need can refer to it.

PHPqrCode is a PHP 2D code. Code generation class library, you can use it to easily generate QR codes. The official website provides downloads and multiple demonstration demos.

After downloading the class library provided by the official website, you only need to use phpqrcode.php to generate QR codes. Yes, of course your PHP environment must enable support for GD2. phpqrcode.php provides a key png() method, in which the parameter $text indicates the generation of two-digit information text; the parameter $outfile indicates whether to output a QR code image file, the default is no; the parameter $level indicates the fault tolerance rate, that is The covered areas can still be identified, which are L (QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25%), H (QR_ECLEVEL_H, 30%); The parameter $size indicates the size of the generated image. , the default is 3; the parameter $margin indicates the spacing value of the blank area of ​​the border around the QR code; the parameter $saveandprint indicates whether to save the QR code and display it.

Calling PHP qrCode is very simple. The following code can generate a QR code with the content "http://www.learnphp.cn".

Php code

include 'phpqrcode.php';   
QRcode::png('http://www.php.cn');

In actual application, we will add our own LOGO in the middle of the QR code to enhance the publicity effect. So how to generate a QR code containing a logo? In fact, the principle is very simple. First use PHP qr Code to generate a QR code image, and then use PHP's image related functions to add the pre-prepared logo image to the middle of the original QR code image just generated, and then regenerate a new one. QR code picture.

<?php 
include &#39;phpqrcode.php&#39;; 
$value = &#39;http://www.php.cn&#39;; //二维码内容 
$errorCorrectionLevel = &#39;L&#39;;//容错级别 
$matrixPointSize = 6;//生成图片大小 
//生成二维码图片 
QRcode::png($value, &#39;qrcode.png&#39;, $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = &#39;logo.png&#39;;//准备好的logo图片 
$QR = &#39;qrcode.png&#39;;//已经生成的原始二维码图 
  
if ($logo !== FALSE) { 
$QR = imagecreatefromstring(file_get_contents($QR)); 
$logo = imagecreatefromstring(file_get_contents($logo)); 
$QR_width = imagesx($QR);//二维码图片宽度 
$QR_height = imagesy($QR);//二维码图片高度 
$logo_width = imagesx($logo);//logo图片宽度 
$logo_height = imagesy($logo);//logo图片高度 
$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; 
//重新组合图片并调整大小 
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, 
$logo_qr_height, $logo_width, $logo_height); 
} 
//输出图片 
imagepng($QR, &#39;helloweba.png&#39;); 
echo &#39;<img src="helloweba.png">&#39;; 
?>

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

php implements socket push technology

How to use Elasticsearch in PHP

The above is the detailed content of Generate QR code using PHP class library PHPqrCode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn