当前位置:网站首页>PHP uses endroid/qrcode QR code to generate, and Gd library generates sharing posters
PHP uses endroid/qrcode QR code to generate, and Gd library generates sharing posters
2022-06-28 23:25:00 【supramolecular】
design sketch :

1. Use composer install endroid/qrcode
github Address :https://github.com/endroid/qr-code
composer require endroid/qrcode2. install php install gd Expand
gd Extended reference manual :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 Get the size of the background image
list($bg_w,$bg_h) = getimagesize($this->imgFolder."/bg.jpg");
// 2 Create a picture
$img = @imagecreatetruecolor($bg_w,$bg_h);
// 3 Fill in the canvas background color
$img_bg_color = imagecolorallocate($img,255,255,255);
imagefill($img,0,0,$img_bg_color);
// 4 Fill the background image into the canvas
$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 Generate qr code , Fill in user QR code
$qecodeName = $this->generateQrcode($url);
$qrcode = $this->getImgReource($qecodeName);
// Get QR code size
list($qr_w,$qr_h) = getimagesize($qecodeName);
imagecopyresized($img,$qrcode,40,638,0,0,220,220,$qr_w,$qr_h);
// 6 Fill in the user image
$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);
// Fill in the blank user name
$font_color = ImageColorAllocate($img,0,0,0); // The font color
$font_ttf = $this->fontFolder. "/MSYHB.ttf";
$font_ttf2 = $this->fontFolder. "/MSYHL.ttc";
imagettftext($img,32,0,205,430,$font_color,$font_ttf,$userName);
// 7 Set title
imagettftext($img,24,0,205,490,$font_color,$font_ttf2,$title);
// 8 Save the picture
$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;
}
/**
* Get image file resources
* @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;
}
}
边栏推荐
- 再次上榜!知道创宇入选2022中国网安产业竞争力50强
- 笔记
- Class extension and optional type extension of dart
- 设计电商秒杀系统
- Non scientific class! The road of self-study!
- IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
- Online sql to htmltable tool
- Hesitating root sound
- That's how he did it!
- 移动端异构运算技术 - GPU OpenCL 编程(基础篇)
猜你喜欢
随机推荐
[sword finger offer] 50 First character that appears only once
Chapter V virtual memory exercise
Count the number of arrays with pointers
[opencv] - linear filtering: box filtering, mean filtering, Gaussian filtering
TDD and automated testing
两栏布局左边图片显示部分由右边内容高度决定
在DialogFragment的onStop(完全不可见)时调用dismiss退出界面报错解决办法
收藏 | VLOOKUP函数的这些妙用你都知道吗?
urllib.parse 解析url连接中的参数
第四章 存储器管理练习
Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
Fanuc robot_ Introduction to Karel programming (2)_ Usage of general IO signal
MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数
Machine learning 6-decision tree
Mathematical knowledge: finding combinatorial number I - finding combinatorial number
frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
TDD案例实战
小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
C# 面试题目_20220627记录一下
With a monthly salary of 60000 yuan, such people began to be robbed after the Internet "reduced costs and increased efficiency"



![[matlab] function definition and use](/img/43/a7970ca8e075151277f7773434f7db.png)


![[stm32 HAL库] RTC和BKP驱动](/img/72/c2c46377d0a2a5a032802640ca0201.png)


