PHP QR Code 生成二维码
PHP /
2016-01-08 /
阅读: 17
最近在开发一个项目要用到生成二维码功能,从网上搜了搜, PHP QR Code就能够实现这个功能。
首先,它的官网:
http://phpqrcode.sourceforge.net/
可以从这里下载:
http://sourceforge.net/projects/phpqrcode
使用的方法很简单,简单使用,下面的代码就可以实现:
<?php
//文件输出
include('phpqrcode/phpqrcode.php');
// 二维码数据
$data = 'http://www.leocode.net';
// 生成的文件名
$filename = 'test.png';
// 纠错级别:L、M、Q、H
$errorCorrectionLevel = 'L';
// 点的大小:1到10
$matrixPointSize = 4;
QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
?>也可以结合生成中间有LOGO的二维码:
<?php
include ('phpqrcode/phpqrcode.php');
$value = 'http://www.leocode.net';
$errorCorrectionLevel = 'L';
$matrixPointSize = 10;
QRcode::png ( $value, 'test.png', $errorCorrectionLevel, $matrixPointSize, 2 );
echo "QR code generated" . "<br />";
$logo = 'logo.gif'; // LOGO的路径
$QR = 'test.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, 'result.png' );
?>