分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

Thinkphp3.2结合phpqrcode生成二维码(含Logo的二维码),附案例

发布时间:2023-09-06 01:12责任编辑:沈小雨关键词:二维码

首先,下载phpqrcode,将其解压到项目ThinkPHP\Library\Vendor目录下。
Index_index.html(模板可自行配置)

<form action="{:U(‘index/index‘)}" enctype="multipart/form-data" ?method = "post"> ???<b>尺寸大小:</b> ???????<input type="text" name = "matrixPointSize"> ???<br> ???<b>边距大小:</b> ???????<select class="form-control" name="matrixMarginSize" > ???????????<option value="1" selected="selected">1px</option> ???????????<option value="2">2px</option> ???????????<option value="3">3px</option> ???????????<option value="4">5px</option> ???????</select><br> ???<b>容错级别:</b> ???????<select class="form-control" name="errorCorrectionLevel" id="errorCorrectionLevel"> ???????<option value="L">Low (7%)</option> ???????<option value="M" selected="selected">Medium (15%)</option> ???????<option value="Q">Quartile (25%)</option> ???????<option value="H">High (30%)</option> ???????</select><br> ???<b>二维码内容:</b> ???????<input type="text" name = "content"> ???<br> ???<b>二维码logo:</b> ???????<input type="file" name = "test" /><br><br> ???<input type="submit" value = "生成二维码"></form>
<img src="{$img}" >

控制器:IndexController.class.php

<?phpnamespace 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时生成的二维码:

下图即在有图片被上传的时候生成的二维码:

案例源码下载:ThinkPHP_3.2.2.zip

pgpqrcode下载:phpqrcode.rar

Thinkphp3.2结合phpqrcode生成二维码(含Logo的二维码),附案例

原文地址:http://www.cnblogs.com/zfy0818/p/7543729.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved