经验
使用PHP生成二维码的方法(带logo图像)
PHP QR Code是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo,查看地址:http://phpqrcode.sourceforge.net/。
下载官网提供的类库后,只需要使用phpqrcode.php就可以生成二维码了,当然您的PHP环境必须开启支持GD2。 phpqrcode.php提供了一个关键的png()方法,其中参数$text表示生成二位的的信息文本;参数$outfile表示是否输出二维码图片 文件,默认否;参数$level表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%); 参数$size表示生成图片大小,默认是3;参数$margin表示二维码周围边框空白区域间距值;参数$saveandprint表示是否保存二维码并 显示。
1 2 3 4 5 6 | 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);
}
|
调用PHP QR Code非常简单,如下代码即可生成一张内容为"http://www.jb51.net"的二维码.
Php代码
include 'phpqrcode.php';
QRcode::png('http://www.jb51.net');
那么实际应用中,我们会在二维码的中间加上自己的LOGO,已增强宣传效果。那如何生成含有logo的二维码呢?其实原理很简单,先使用PHP QR Code生成一张二维码图片,然后再利用php的image相关函数,将事先准备好的logo图片加入到刚生成的原始二维码图片中间,然后重新生成一张新 的二维码图片。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | include 'phpqrcode.php' ;
$errorCorrectionLevel = 'L' ;
$matrixPointSize = 6;
QRcode::png( $value , 'qrcode.png' , $errorCorrectionLevel , $matrixPointSize , 2);
$logo = 'logo.png' ;
$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_height = imagesy( $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.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
include 'phpqrcode.php' ;
$value = $_GET [ 'url' ];
$errorCorrectionLevel = 'L' ;
$matrixPointSize = 6;
QRcode::png( $value , 'qrcode.png' , $errorCorrectionLevel , $matrixPointSize , 2);
$logo = 'jb51.png' ;
$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_height = imagesy( $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 );
|
调用方法:

由于二维码允许有一定的容错性,一般的二维码即使在遮住部分但仍然能够解码,经常我们扫描二维码的时候扫描到甚至不到一半时就能解码扫描结果,这是因为生成器会将部分信息重复表示来提高其容错度,这就是为什么我们在二维码中间加个LOGO图片并不影响解码结果的原因。
下载地址:http://www.weinidai.com/sources/download/phpqrcode-2010100721_1.1.4生成二维码的类库.zip