前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报

PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报

作者头像
IT工作者
发布2021-12-20 18:11:10
1.6K0
发布2021-12-20 18:11:10
举报
文章被收录于专栏:程序技术知识程序技术知识

1. 使用composer安装 endroid/qrcode

github地址:https://github.com/endroid/qr-code

composer require endroid/qrcode

2. 安装 php安装gd扩展

gd扩展参考手册:https://www.php.net/manual/zh/book.image.php

<?php

namespace app\index\controller;

use Endroid\QrCode\QrCode;

class Poste

{

function __construct($imgFolder, $fontFolder) {

$this->imgFolder = $imgFolder;

$this->fontFolder = $fontFolder;

}

private $imgFolder;

private $fontFolder;

public function create($url, $userName, $title)

{

// 1 获取背景图尺寸

list($bg_w,$bg_h) = getimagesize($this->imgFolder."/bg.jpg");

// 2 创建画图

$img = @imagecreatetruecolor($bg_w,$bg_h);

// 3 填充画布背景颜色

$img_bg_color = imagecolorallocate($img,255,255,255);

imagefill($img,0,0,$img_bg_color);

// 4 将背景图填充到画布

$bg_img = $this->getImgReource($this->imgFolder."/bg.jpg");

imagecopyresized($img,$bg_img,0,0,0,0,$bg_w,$bg_h,$bg_w,$bg_h);

// 5 生成二维码, 填充用户二维码

$qecodeName = $this->generateQrcode($url);

$qrcode = $this->getImgReource($qecodeName);

// 获取二维码尺寸

list($qr_w,$qr_h) = getimagesize($qecodeName);

imagecopyresized($img,$qrcode,40,638,0,0,220,220,$qr_w,$qr_h);

// 6 填充用户图像

$user_img = $this->getImgReource($this->imgFolder."/head.png");

list($user_w,$user_h) = getimagesize($this->imgFolder."/head.png");

imagecopyresized($img,$user_img,40,380,0,0,130,130,$user_w,$user_h);

// 填空用户名

$font_color = ImageColorAllocate($img,0,0,0); //字体颜色

$font_ttf = $this->fontFolder. "/MSYHB.ttf";

$font_ttf2 = $this->fontFolder. "/MSYHL.ttc";

imagettftext($img,32,0,205,430,$font_color,$font_ttf,$userName);

// 7 设置标题

imagettftext($img,24,0,205,490,$font_color,$font_ttf2,$title);

// 8 保存图片

$posterName = $this->imgFolder."/".date("YmdHis", time()).$this->generateRandomString().".png";

imagepng($img,$posterName);

return $posterName;

}

private function generateQrcode($url)

{

$qrCode = new QrCode($url);

$qrCode->setSize(300);

$qrCode->setWriterByName('png');

$qrCode->setMargin(10);

$qrCode->setEncoding('UTF-8');

// $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());

$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);

$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);

$qrCode->setLogoSize(150, 200);

$qrCode->setRoundBlockSize(true);

$qrCode->setValidateResult(false);

$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);

print_r(gettype($qrCode->writeString()));

$qecodeName = $this->imgFolder."/".date("YmdHis", time()).$this->generateRandomString().".png"; // 2017-12-14 23:13:51

$qrCode->writeFile($qecodeName);

return $qecodeName;

}

private function generateRandomString($length = 10) {

$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

$randomString = '';

for ($i = 0; $i < $length; $i++) {

$randomString .= $characters[rand(0, strlen($characters) - 1)];

}

return $randomString;

}

/**

* 获取图像文件资源

* @param string $file

* @return resource

*/

protected function getImgReource($file){

$file_ext = pathinfo($file,PATHINFO_EXTENSION);

switch ($file_ext){

case 'jpg':

case 'jpeg':

$img_reources = @imagecreatefromjpeg($file);

break;

case 'png':

$img_reources = @imagecreatefrompng($file);

break;

case 'gif':

$img_reources = @imagecreatefromgif($file);

break;

}

return $img_reources;

}

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档