This article mainly shares with you two methods of generating QR codes with logo images in PHP. It is mainly shared with you in the form of text and code. I hope it can help you.
1. Generate QR code using Google API
Google provides a relatively complete QR code generation interface. It is very simple to call the API interface. The following is the calling code:
$urlToEncode="http://www.php.cn"; generateQRfromGoogle($urlToEncode); /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param string $chl 二维码包含的信息,可以是数字、字符、二进制信息、汉字。 不能混合数据类型,数据必须经过UTF-8 URL-encoded * @param int $widhtHeight 生成二维码的尺寸设置 * @param string $EC_level 可选纠错级别,QR码支持四个等级纠错,用来恢复丢失的、读错的、模糊的、数据。 * L-默认:可以识别已损失的7%的数据 * M-可以识别已损失15%的数据 * Q-可以识别已损失25%的数据 * H-可以识别已损失30%的数据 * @param int $margin 生成的二维码离图片边框的距离 */ function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') { $chl = urlencode($chl); echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.' &cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$widhtHeight.' " widhtHeight="'.$widhtHeight.'"/>'; }
2. Use the PHP QR code generation library PHP QR Code to generate QR codes
PHP QR Code is a PHP QR code generation library that can be used to easily generate QR codes. The official website provides downloads And multiple demos, check the address: http://phpqrcode.sourceforge.net/.
After downloading the class library provided by the official website, you only need to use phpqrcode.php to generate the QR code. Of course, your PHP environment must enable GD2 support. phpqrcode.php provides a key png() method, in which the parameter text indicates the generation of two-digit information text; 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 represents the fault tolerance rate, that is, the covered area can still be recognized, respectively L (QRECLEVELL, 7level represents the fault tolerance rate, that is, the covered area can still be recognized, respectively L (QRECLEVELL, 7size represents the size of the generated image , the default is 3; the parameter margin represents the spacing value of the blank area of the border around the QR code; the parameter margin represents the spacing value of the blank area of the border around the QR code; the parameter saveandprint represents whether to save the QR code and display it.
public static function png($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); }
Call. PHP QR Code is very simple. The following code can generate a QR code with the content "http://www.jb51.net".
Php code
include 'phpqrcode.php';
QRcode ::png('http://www.jb51.net');
In actual application, we will add our own LOGO in the middle of the QR code to enhance the publicity effect. What about the QR code containing the 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 function to add the pre-prepared logo image to the newly generated original QR code image. in the middle, and then regenerate a new QR code image.
include 'phpqrcode.php'; $value = 'http://www.jb51.net'; //二维码内容 $errorCorrectionLevel = 'L';//容错级别 $matrixPointSize = 6;//生成图片大小 //生成二维码图片 QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'logo.png';//准备好的logo图片 $QR = 'qrcode.png';//已经生成的原始二维码图 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, 'helloweba.png'); echo '<img src="helloweba.png">';
The following is a reference to the above code. It does not produce image files and is convenient for calling. Save the following code as img.php
<?php include 'phpqrcode.php'; $value = $_GET['url'];//二维码内容 $errorCorrectionLevel = 'L';//容错级别 $matrixPointSize = 6;//生成图片大小 //生成二维码图片 QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'jb51.png';//准备好的logo图片 $QR = 'qrcode.png';//已经生成的原始二维码图 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); } //输出图片 Header("Content-type: image/png"); ImagePng($QR);
Since QR codes allow a certain degree of fault tolerance, general QR codes can still be decoded even if they are partially covered. Often when we scan a QR code, we can decode the scan result even less than halfway through. This is because the generator will repeatedly represent part of the information to improve its fault tolerance, which is why adding a LOGO image in the middle of the QR code does not affect the decoding result.
Related recommendations:
php implements the QR code class with logo
PHP generates WeChat QR code example
php QR code Production and download methods
The above is the detailed content of Two ways to generate QR code with logo image using PHP. For more information, please follow other related articles on the PHP Chinese website!