当前位置:网站首页>PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报
PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报
2022-06-28 23:21:00 【supramolecular】
效果图:

1. 使用composer安装 endroid/qrcode
github地址:https://github.com/endroid/qr-code
composer require endroid/qrcode2. 安装 php安装gd扩展
gd扩展参考手册:https://www.php.net/manual/zh/book.image.php
<?php
namespace app\index\controller;
use Endroid\QrCode\QrCode;
class Poster
{
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;
}
}
边栏推荐
- Is it difficult to register stocks and open accounts online? Is it safe to open an account online?
- [sword finger offer] 50 First character that appears only once
- CIN at QT (the clearest tutorial in the whole network)
- 机器学习6-决策树
- 2022-06-28:以下golang代码输出什么?A:true;B:false;C:panic;D:编译失败。 package main import “fm
- 全面掌握const的用法《一》
- Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
- Is it safe to open a stock account online?
- The picture display on the left of the two column layout is determined by the content height on the right
- Go language - reflect
猜你喜欢
随机推荐
Online sql to htmltable tool
[matlab]函数定义与使用
生产环境sonarqube安装
Do you know all the wonderful functions of the vlookup function?
Matlab learning notes (6) upsample function and downsample function of MATLAB
Chapter III processor scheduling exercise
When dialogfragment's onstop is completely invisible, call disass to exit the interface and report an error. Solution
YuMinHong set up two funds funded by his hometown
C language - word analysis
【狀態機設計】Moore、Mealy狀態機、三段式、二段式、一段式狀態機書寫規範
2022年PMP项目管理考试敏捷知识点(4)
月薪6万,互联网“降本增效”后,这类人开始被疯抢
没找到实习,他总结了这些
Finally, someone explained the cloud native architecture
两栏布局左边图片显示部分由右边内容高度决定
Class extension and optional type extension of dart
Three communication skills in software testing
再次上榜!知道创宇入选2022中国网安产业竞争力50强
【SSM】报错 Access denied for user ‘WYF‘@‘localhost‘ (using password: YES) 数据的用户名变成了电脑的用户名
复杂嵌套的对象池(4)——管理多类实例和多阶段实例的对象池



![[数学建模]Matlab非线性规划之fmincon()函数](/img/fc/46949679859b1369fcc83d0d8b637c.png)




