当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

Tanghongbin, Yaya live CTO: to truly localize, the product should not have the attribute of "origin"

2022 PMP project management examination agile knowledge points (4)

机器学习4-降维技术
![LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路](/img/41/b8ba8d771b7224eac1cc8c54fe9d29.png)
LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路

ROS2中的行为树 BehaviorTree

window10 phpstudy 安装redis扩展

One card can sell tens of millions, and the business expansion is faster than that of players: you don't understand the Jianghu of star cards
![[stm32 Hal library] RTC and BKP drives](/img/72/c2c46377d0a2a5a032802640ca0201.png)
[stm32 Hal library] RTC and BKP drives

一张能卖上千万,商家扩张比玩家还快:球星卡的江湖你不懂

Interviewer: what is the internal implementation of strings in redis?
随机推荐
fio的IO重放功能
2022-06-28:以下golang代码输出什么?A:true;B:false;C:panic;D:编译失败。 package main import “fm
frameworks/base/core/res/res/values/symbols. Xml:3915: error: no definition for declared symbol solution
Fanuc robot_ Introduction to Karel programming (2)_ Usage of general IO signal
见丰知夏|国漫鼻祖丰子恺,数字藏品独家发售
图片64base转码与解码
Web API learning notes 1
Finally, someone explained the cloud native architecture
【OpenCV】—线性滤波:方框滤波、均值滤波、高斯滤波
Be on the list again! Know that Chuangyu was selected as one of the top 50 competitive enterprises in China's network security industry in 2022
Behaviortree in ros2
LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路
Puma joins hands with 10ktf shop to launch its Web3 cooperation project with the largest scale so far
大三,不简单啊!
[word Tutorial Series Part 1] how to remove arrows in word tables
Association line exploration, how to connect the two nodes of the flow chart
MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数
百度知道爬虫,根据问题id,线索id,评论id获取评论下面的对话
pymysql.Error 获取错误码与具体错误信息
PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报