ThinkPHP3.2.3扩展之二维码,thinkphp3.2.3扩展_PHP教程

php中文网
发布: 2016-07-12 09:00:31
原创
1740人浏览过

thinkphp3.2.3扩展之二维码,thinkphp3.2.3扩展

先安照路径放好如图。

简单使用无logo:

public function qrcode(){
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $url='http://www.shouce.ren/';//网址或者是文本内容
        $level=3;
        $size=4;
        $errorCorrectionLevel =intval($level) ;//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }
登录后复制

高级使用带logo:

public function qrcode(){
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $qrcode_path='';
        $file_tmp_name='';
        $errors=array();
        if(!empty($_POST)){
            $content = trim($_POST['content']); //二维码内容
            $contentSize=$this->getStringLength($content);
            if($contentSize>150){
                $errors[]='字数过长,不能多于150个字符!';
            }
            if(isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])){
                if($_FILES['upimage']['size']>512000){
                    $errors[]="你上传的文件过大,最大不能超过500K。";
                }
                $file_tmp_name=$_FILES['upimage']['tmp_name'];
                $fileext = array("image/pjpeg","image/jpeg","image/gif","image/x-png","image/png");
                if(!in_array($_FILES['upimage']['type'],$fileext)){
                    $errors[]="你上传的文件格式不正确,仅支持 png, jpg, gif格式。";
                }
            }
            $tpgs=$_POST['tpgs'];//图片格式
            $qrcode_bas_path='upload/qrcode/';
            if(!is_dir($qrcode_bas_path)){
                mkdir($qrcode_bas_path, 0777, true);
            }
            $uniqid_rand=date("Ymdhis").uniqid(). rand(1,1000);
            $qrcode_path=$qrcode_bas_path.$uniqid_rand. "_1.".$tpgs;//原始图片路径
            $qrcode_path_new=$qrcode_bas_path.$uniqid_rand."_2.".$tpgs;//二维码图片路径
            if(Helper::getOS()=='Linux'){
                $mv = move_uploaded_file($file_tmp_name, $qrcode_path);
            }else{
                //解决windows下中文文件名乱码的问题
                $save_path = Helper::safeEncoding($qrcode_path,'GB2312');
                if(!$save_path){
                    $errors[]='上传失败,请重试!';
                }
                $mv = move_uploaded_file($file_tmp_name, $qrcode_path);
            }
            if(empty($errors)){
                $errorCorrectionLevel = $_POST['errorCorrectionLevel'];//容错级别
                $matrixPointSize = $_POST['matrixPointSize'];//生成图片大小
                $matrixMarginSize = $_POST['matrixMarginSize'];//边距大小
                //生成二维码图片
                $object::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize);
                $QR = $qrcode_path_new;//已经生成的原始二维码图
                $logo = $qrcode_path;//准备好的logo图片
                if (file_exists($logo)) {
                    $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图片宽度
                    $logo_height = imagesy($logo);//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,$qrcode_path);
                    imagedestroy($QR);
                }else{
                    $qrcode_path=$qrcode_path_new;
                }
            }else{
                $qrcode_path='';
            }
        }
        $data=array('data'=>array('errors'=>$errors,'qrcode_path'=>$qrcode_path));
        $this->assign('data',$data);
        $this->display();
登录后复制

演示地址:http://www.shouce.ren/tool/qrcode

文心快码
文心快码

文心快码(Comate)是百度推出的一款AI辅助编程工具

文心快码 35
查看详情 文心快码

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

用到助手类Helper地址:http://www.thinkphp.cn/topic/34875.html

  

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1093104.htmlTechArticleThinkPHP3.2.3扩展之二维码,thinkphp3.2.3扩展 先安照路径放好如图。 简单使用无logo: public function qrcode(){ Vendor('phpqrcode.phpqrcode'); //生成二维码...
相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

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

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

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