美文网首页
TP5做二维码(带拼图的)

TP5做二维码(带拼图的)

作者: 涳_de26 | 来源:发表于2019-05-23 17:10 被阅读0次

    用TP5框架做一个二维码(可以加拼图的)

        /**
         * 我的二维码
         */
        public function userQrcode(){
            $uid = $this->user_id;      //获取用户ID
            $filename = $uid.'.png';    //设置二维码文件名
            $f = config('shop_directory').config('qrcode_path').$uid.DS.$filename;      //二维码生成路径
            if(!(is_file($f)) || !file_exists($f)){     //判断二维码是否存在
                $thisurl = "https://www.fasene.cn/flower_shop_ser/qrcode?first_leader=".$uid;       //二维码链接
                $this->mark_phpqrcode($f,$thisurl);             //生成二维码方法
            }
            //查看是否有拼图
            $info_filename = 'share.png';           //查看二维码拼图
            $shareuidname = 'ushare'.$this->user_id.'png';      //生成后的加上拼图的二维码文件名
            //拼图二维码
            $shareuid = config('shop_directory').config('qrcode_path').$uid.DS.$shareuidname;       //加上拼图的二维码路径
            $info_f = config('shop_directory').config('qrcode_path').$info_filename;        //拼图路径
            if(!(is_file($shareuid)) || !file_exists($shareuid) || !(is_file($info_f)) || !file_exists($info_f)){       //判断拼图和加上拼图的二维码是否存在
                $this->mark_infoqrcode();       //生成拼图二维码方法
            }
            return json(config('shop_domain').config('qrcode_path').$uid.'/ushare'.$uid.'.png',200);        //返回拼图二维码图片链接
        }
    
        /**
         * 生成二维码
         * @param string $filename
         * @param string $thisurl
         */
        public function mark_phpqrcode($filename="",$thisurl=''){
            $uid = $this->user_id;          //用户ID(防止没有二维码链接,生成默认链接)
            $img = $uid.'.png';             //二维码文件名(防止没有二维码链接,生成默认链接)
            if(empty($filename)) $filename = config('shop_directory').config('qrcode_path').$uid.DS.$img;       //如果没有链接,生成默认链接
    
            ob_end_clean();
            vendor('phpqrcode.phpqrcode');      //引入生成二维码的第三方类(网上直接下载)
            error_reporting(E_ERROR);           //设置报错信息
            // 二维码数据
            if(empty($thisurl)){        //进一步判断
                $this->error('分销二维码链接错误,请联系管理员!!!');
            };
    
            // 生成的文件名
            checkDir($filename);        //生成二维码路径文件
    
            // 纠错级别:L、M、Q、H
            $errorCorrectionLevel = 'L';
            // 点的大小:1到10
            $matrixPointSize = 6;
            \QRcode::png($thisurl, $filename, $errorCorrectionLevel, $matrixPointSize, 2);      //生成二维码
        }
    
    
    
        /**
         * 二维码拼图
         * @param int $wxuid
         * @return mixed
         */
        public function mark_infoqrcode(){
            $uid = $this->user_id;        //用户ID
            //带背景拼图
            $QR = config('shop_directory').config('qrcode_path').'share.png';   //拼图图片路径
    
            $img="ushare".$uid.'.png';      //拼图加二维码图片文件名
            $logo = config('shop_directory').config('qrcode_path').$uid.DS.$uid.'.png';     //二维码图片路劲
            $logo2= config('shop_directory').config('qrcode_path').$uid.DS.$img;            //拼图加二维码图片路径
            if(!(is_file($QR)) || !file_exists($QR) || (is_file($logo2)) || (file_exists($logo2))){             //判断拼图加二维码图片是否存在
                return 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图片宽度
            $logo_height = imagesy($logo);//logo图片高度
    
            $logo_qr_width = $QR_width / 2;
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            $from_width= 238;           //二维码出现的位置  x轴
            $from_y = 735;              //二维码出现的位置  Y轴
            $logo_qr_width = 100;       //二维码宽度
            $logo_qr_height = 100;      //二维码高度
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_y, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
            $img = config('shop_directory').config('qrcode_path').$uid.DS.$img;
            imagepng($QR,$img);     //生成带二维码的拼图
        }
    
    /**
     * 生成路径
     * @param $fn
     * @return bool
     */
    function checkDir($fn){
        if(empty($fn)) die('设置的路径为空!');
        $fn = str_replace('/',DS,$fn);
        $pathArr = pathinfo($fn);
        if(is_dir($pathArr['dirname'])) return true;
        $dirArr = explode(DS, $pathArr['dirname']);
        unset($pathArr);
        $dir = null;
        foreach($dirArr as $folder){
            $dir .= $folder.DS;
            if(!file_exists($dir))@mkdir($dir,0777);
        }
        unset($dirArr);
    }
    

    相关文章

      网友评论

          本文标题:TP5做二维码(带拼图的)

          本文链接:https://www.haomeiwen.com/subject/pvivzqtx.html