thinkphp5.1 生成推广二维码 composer require endroid/qrcode

2020年03月19日 13:21:05阅读数:511博客 / 魔十七 / 程序笔记

例子: 版本 php7.0 thinkphp5.1一. 使用composer安装扩展 composer require endroid/qrcode二.引用扩展 封装成一个类 例:

<?php/** * User: 魔十七 * Date: 2020/2/15 * Time: 13:32 */namespace app\index\controller;use Endroid\QrCode\ErrorCorrectionLevel;use Endroid\QrCode\LabelAlignment;use Endroid\QrCode\QrCode;/** * composer require endroid/qrcode * Class QrcodeServer 生成二维码 * @package app\index\controller */class QrcodeServer{ protected $_qr; protected $_encoding = 'UTF-8'; // 编码类型 protected $_size = 300; // 二维码大小 protected $_logo = false; // 是否需要带logo的二维码 protected $_logo_url = ''; // logo图片路径 protected $_logo_size = 80; // logo大小 protected $_title = false; // 是否需要二维码title protected $_title_content = ''; // title内容 protected $_generate = 'display'; // display-直接显示 writefile-写入文件 protected $_file_name = './upload/pic/qrcode'; // 写入文件路径 protected $_image_name = 'ceshi'; // 图片名称 const MARGIN = 10; // 二维码内容相对于整张图片的外边距 const WRITE_NAME = 'png'; // 写入文件的后缀名 const FOREGROUND_COLOR = ['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]; // 前景色 const BACKGROUND_COLOR = ['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]; // 背景色 public function __construct($config) { isset($config['generate']) && $this->_generate = $config['generate']; isset($config['encoding']) && $this->_encoding = $config['encoding']; isset($config['size']) && $this->_size = $config['size']; isset($config['logo']) && $this->_logo = $config['logo']; isset($config['logo_url']) && $this->_logo_url = $config['logo_url']; isset($config['logo_size']) && $this->_logo_size = $config['logo_size']; isset($config['title']) && $this->_title = $config['title']; isset($config['title_content']) && $this->_title_content = $config['title_content']; isset($config['file_name']) && $this->_file_name = $config['file_name']; isset($config['image_name']) && $this->_image_name = $config['image_name']; } /** * 生成二维码 * @param $content //需要写入的内容 * @return array | page input */ public function createServer($content) { $this->_qr = new QrCode($content); $this->_qr->setSize($this->_size); $this->_qr->setWriterByName(self::WRITE_NAME); $this->_qr->setMargin(self::MARGIN); $this->_qr->setEncoding($this->_encoding); $this->_qr->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH); // 容错率 $this->_qr->setForegroundColor(self::FOREGROUND_COLOR); $this->_qr->setBackgroundColor(self::BACKGROUND_COLOR); // 是否需要title if ($this->_title) { $this->_qr->setLabel($this->_title_content, 16, null, LabelAlignment::CENTER); } // 是否需要logo if ($this->_logo) { $this->_qr->setLogoPath($this->_logo_url); $this->_qr->setLogoWidth($this->_logo_size); } $this->_qr->setValidateResult(false); if ($this->_generate == 'display') { // 展示二维码 // 前端调用 例:<img src="http://localhost/qr.php?url=base64_url_string"> header('Content-Type: ' . $this->_qr->getContentType()); return $this->_qr->writeString(); } else if ($this->_generate == 'writefile') { // 写入文件 $file_name = $this->_file_name; return $this->generateImg($file_name); } else { return ['success' => false, 'message' => 'the generate type not found', 'data' => '']; } } /** * 生成文件 * @param $file_name //目录文件 例: /tmp * @return array */ public function generateImg($file_name) { $file_path = $file_name . DIRECTORY_SEPARATOR . $this->_image_name . '.' . self::WRITE_NAME; if (!file_exists($file_name)) { mkdir($file_name, 0777, true); } try { $this->_qr->writeFile($file_path); $data = [ 'url' => $file_path, 'ext' => self::WRITE_NAME, ]; return ['success' => true, 'message' => 'write qrimg success', 'data' => $data]; } catch (\Exception $e) { return ['success' => false, 'message' => $e->getMessage(), 'data' => '']; } }}

三. 我这里封装成啦公共函数getQRCode()例;

<?php// 应用公共文件use think\Db;use app\index\controller\QrcodeServer;function getQRCode($duan='0',$qr_url='',$info_name='',$type=0,$title=false,$title_content='',$logo=false,$logo_url="",$logo_size='80'){ // 自定义二维码配置 $config = [ 'title' => $title, 'title_content' => $title_content, 'logo' => $logo, 'logo_url' => $logo_url, 'logo_size' => $logo_size, 'image_name' => $info_name, ]; if($type==0){ // 直接输出 $qr_code = new QrcodeServer($config); $qr_img = $qr_code->createServer($qr_url); echo $qr_img; }else{ // 写入文件 if($duan=='0'){ $file_name = './upload/pic/qrcode/pc/'; // 定义保存目录 }else{ $file_name = './upload/pic/qrcode/wap/'; // 定义保存目录 } $config['file_name'] = $file_name; $config['generate'] = 'writefile'; $qr_code = new QrcodeServer($config); $rs = $qr_code->createServer($qr_url); return $rs; }}

四 调用例;

public function my_qrcode(){ $name="wap_"."唯一字符串"; $save_path="./upload/pic/qrcode/wap/$name.png"; if (!file_exists($save_path)) { //背景图 $bgImg='./upload/pic/'.config('fenxiao.fx_pic'); $ewm_location=empty(config('fenxiao.ewm_location')) ? 143*295 : config('fenxiao.ewm_location'); //调用 getQRCode(1,'扫码地址?pid='.$m_uid,$name,'1'); $backgroupImg = imagecreatefromstring(file_get_contents($bgImg)); $newQR = imagecreatefromstring(file_get_contents($save_path)); //获取新的尺寸 list($width, $height) = getimagesize($save_path); $new_width = 300; $new_height = 300; //重新组合图片并调整大小 $weizhi=explode('*',$ewm_location); imagecopyresampled($backgroupImg,$newQR,$weizhi[0], $weizhi[1], 0, 0,$new_width, $new_height, $width, $height);//输出图片 imagepng($backgroupImg,$save_path); } $this->assign('qrcode','qrcode/wap/'.$name.'.png'); return $this->fetch('/my_qrcode'); }

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
暂无评论暂无评论!
  • 博主信息
    魔十七
    博文
    1
    粉丝
    0
    评论
    0
    访问量
    518
    积分:0
    P豆:4