Two ways to generate QR code using PHP (with logo image) submission: Junjie font: [Increase decrease] type: Reproduced with the progress of science and technology, QR Code application field more and more widely, today I give you to share how to use PHP to generate two-dimensional code, and how to generate a logo image in the middle of the two-dimensional code
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.jb51.net"
;
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
‘
.
$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, where the parameter $text represents the generation of two-bit information text, the parameter $outfile indicates whether to output a QR code picture file, the default is no, the parameter $level indicates fault tolerance, that is, the covered area can be recognized, respectively L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_h,30%), parameter $size indicates the image size is generated, default is 3; Margin indicates the spacing value of the border space around the QR code, and the parameter $saveandprint indicates 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);
}
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‘
; //二维码内容
$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
‘‘
;
Here is the reference to the above code, do not produce image files, convenient to call, 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
);
Call Method:
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.
Two methods for generating two-dimensional code using PHP (with logo image)