用Thinkphp3.2结合phpqrcode生成二维码

一个新手
发布: 2017-09-28 10:15:55
原创
2876人浏览过

首先,下载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时生成的二维码:

立即学习PHP免费学习笔记(深入)”;

码上飞
码上飞

码上飞(CodeFlying) 是一款AI自动化开发平台,通过自然语言描述即可自动生成完整应用程序。

码上飞138
查看详情 码上飞

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

 

以上就是用Thinkphp3.2结合phpqrcode生成二维码的详细内容,更多请关注php中文网其它相关文章!

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号