首先,下载phpqrcode,将其解压到项目thinkphp\library\vendor目录下。
index_index.html(模板可自行配置)
尺寸大小:
边距大小:
1px
2px
3px
5px
容错级别:
low (7%)
medium (15%)
quartile (25%)
high (30%)
二维码内容:
二维码logo:
控制器:indexcontroller.class.php
<?php
namespace home\controller;
use think\controller;
class indexcontroller extends controller {
public function index() {
$config = array( 'maxsize' => 3145728, 'rootpath' => './upload/', 'savepath' => '', 'savename' => array('uniqid',''), 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autosub' => true, 'subname' => array('date','ymd'), );
$upload = new \think\upload($config);
// 实例化上传类 // 上传单个文件 $info = $upload->uploadone($_files['test']);
if(!$info) {
// 上传错误提示错误信息 $qrcode_path_new = './public/home/images/code'.'_'.date("ymdhis").'.png';
$content = $_post['content'];
$matrixpointsize = $_post['matrixpointsize'];
$matrixmarginsize = $_post['matrixmarginsize'];
$errorcorrectionlevel = $_post['errorcorrectionlevel'];
makecode_no_pic($content,$qrcode_path_new,$matrixpointsize,$matrixmarginsize,$errorcorrectionlevel);
$this->assign('img',$qrcode_path_new);
}
else {
$qrcode_path = "./upload/".$info['savepath'].$info['savename'];
$content = $_post['content'];
$matrixpointsize = $_post['matrixpointsize'];
$matrixmarginsize = $_post['matrixmarginsize'];
$errorcorrectionlevel = $_post['errorcorrectionlevel'];
$url = "./upload/qrcode/".date("ymdhis").".png";
makecode($qrcode_path,$content,$matrixpointsize,$matrixmarginsize,$errorcorrectionlevel,$url);
$this->assign('img',$url);
}
$this->display();
}
}
然后在项目application/home/common下新建function.php
<?phpfunction makecode($qrcode_path,$content,$matrixpointsize,$matrixmarginsize,$errorcorrectionlevel,$url) {
/** 参数详情: * $qrcode_path:logo地址 * $content:需要生成二维码的内容 * $matrixpointsize:二维码尺寸大小 * $matrixmarginsize:生成二维码的边距 * $errorcorrectionlevel:容错级别 * $url:生成的带logo的二维码地址 **/
ob_clean ();
vendor('phpqrcode.phpqrcode');
$object = new \qrcode();
$qrcode_path_new = './public/home/images/code'.'_'.date("ymdhis").'.png';
//定义生成二维码的路径及名称 $object::png($content,$qrcode_path_new, $errorcorrectionlevel, $matrixpointsize, $matrixmarginsize);
$qr = imagecreatefromstring(file_get_contents($qrcode_path_new));
//imagecreatefromstring:创建一个图像资源从字符串中的图像流 $logo = imagecreatefromstring(file_get_contents($qrcode_path));
$qr_width = imagesx($qr);
// 获取图像宽度函数 $qr_height = imagesy($qr);
//获取图像高度函数 $logo_width = imagesx($logo);
// 获取图像宽度函数 $logo_height = imagesy($logo);
//获取图像高度函数 $logo_qr_width = $qr_width / 4;
//logo的宽度 $scale = $logo_width / $logo_qr_width;
//计算比例 $logo_qr_height = $logo_height / $scale;
//计算logo高度 $from_width = ($qr_width - $logo_qr_width) / 2;
//规定logo的坐标位置 imagecopyresampled($qr, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
/** imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h ) * 参数详情: * $dst_image:目标图象连接资源。 * $src_image:源图象连接资源。 * $dst_x:目标 x 坐标点。 * $dst_y:目标 y 坐标点。 * $src_x:源的 x 坐标点。 * $src_y:源的 y 坐标点。 * $dst_w:目标宽度。 * $dst_h:目标高度。 * $src_w:源图象的宽度。 * $src_h:源图象的高度。 **/
header("content-type: image/png");
//$url:定义生成带logo的二维码的地址及名称 imagepng($qr,$url);
}
function makecode_no_pic($content,$qrcode_path_new,$matrixpointsize,$matrixmarginsize,$errorcorrectionlevel) {
ob_clean ();
vendor('phpqrcode.phpqrcode');
$object = new \qrcode();
$object::png($content,$qrcode_path_new, $errorcorrectionlevel, $matrixpointsize, $matrixmarginsize);
}
在没有图片上传的情况下,生成我二维码只是单纯的二维码,只有在有logo上传的时候,才会生成带logo的二维码。
如下图,即没有logo时生成的二维码:
下图即在有图片被上传的时候生成的二维码:
以上就是用thinkphp3.2结合phpqrcode生成二维码的详细内容。
