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
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;
}
}