当前位置:网站首页>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;
}
}
边栏推荐
- Thinking about lever
- 这样学习二叉树
- The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
- 表单校验问题——el-select(初始化页面自动触发校验解决办法)
- Chapter III processor scheduling exercise
- Is the account opening of Guosheng securities really safe and reliable
- 想问问,股票开账户如何优惠开户?网上开户安全么?
- 2022-06-28: what does the following golang code output? A:true; B:false; C:panic; D: Compilation failed. package main import “fm
- 云计算的迷路者
- 在DialogFragment的onStop(完全不可见)时调用dismiss退出界面报错解决办法
猜你喜欢

直击产业落地 | 飞桨重磅推出业界首个模型选型工具

生产环境sonarqube安装

Machine learning 4-dimension reduction technology

计数排序和排序的稳定性

Go language - reflect

【OpenCV】—线性滤波:方框滤波、均值滤波、高斯滤波

Chapter II Classic synchronous exercises

Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
![[deep learning] (3) encoder mechanism in transformer, complete pytoch code attached](/img/cb/d385bee7a229e8d11f5fa8af66311f.gif)
[deep learning] (3) encoder mechanism in transformer, complete pytoch code attached

IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
随机推荐
【狀態機設計】Moore、Mealy狀態機、三段式、二段式、一段式狀態機書寫規範
Online linear programming: Dual convergence, new algorithms, and regret bounds
Hesitating root sound
Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
【Flutter 问题系列第 71 篇】Flutter 中 Uint8List 和 Image 之间的相互转换
Machine learning 6-decision tree
[word Tutorial Series Part 1] how to remove arrows in word tables
C語言-單詞分析解析
见丰知夏|国漫鼻祖丰子恺,数字藏品独家发售
Prometeus 2.36.0 new features
Powerful open source API interface visual management platform Yapi
See fengzhixia | FENGZikai, the originator of Guoman, for exclusive sale of Digital Collections
自动化测试的生命周期是什么?
lock4j--分布式锁中间件--使用/实例
LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路
Go language - reflect
TDD and automated testing
ERROR 1067 (42000): Invalid default value for ‘end_time‘ Mysql
note
Interpretation of papers (DCN) towards k-means-friendly spaces: simultaneous deep learning and clustering