thinkphp5.1合成带二维码海报图片
TP5.1 框架生成带二维码的要求海报,并动态显示,不保存在服务器
一、导入PHPqrcode二维码生成包
1.首先使用的后端的框架是php框架tp5.1,注意,tp5.1和原先的tp5在引入第三方库的方式上发生了很大的变化,tp5原来的引入第三方库的 import(“Vendor.Classes.PHPExcel.IOFactory”)或Vendor(‘phpoffice.phpexcel.Classes.PHPExcel.IOFactory’)方法已经不再使用。
2.下载第三方库phpqrcode
3.下载完成后,把phpqrcode文件放到tp5.1的extend目录下
二、生成二维码
/**
* @param string $src 海报图
* @param string $user 用户信息
*/
public function getCode($src,$user){
//导入二维码
require '../extend/phpqrcode/phpqrcode.php';
//二维码地址 根据实际情况配置要求链接
$value = sprintf('https://xxx.xxxxx.com/registered?username=%s',$user['username']);
//配置二维码
$errorCorrectionLevel = 'L'; //容错级别
$matrixPointSize = 4; //生成图片大小,根据海报二维码位置调整
//生成二维码图片
$filename = DIR_PATH."/uploads/code/code".$user['username'].".png";
//二维码生成地址
$file = "/uploads/code/code".$user['username'].".png";
$qrcode = new \QRcode();
$qrcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $filename; //已经生成的原始二维码图片文件
$QR = imagecreatefromstring(file_get_contents($QR));
//获取海报图
$bg = $src;
//图片处理
$image = \think\Image::open('.'.$bg);
//图书打水印文字
$image->water('.'.$file,[105,792])
->text(sprintf('%s',$user['username']),getcwd().'/msyh.ttf',23,'#FDEF06',[20,20]) //图示名称
->save('.'.$file);
//说明
//[105,792] 是图片的位置[x,y]
//msyh.ttf 是字体包,这里是海报图添加图片
//设置头信息
ob_start(); //开启缓存流
echo file_get_contents($filename); //获取并输出物理图片
$content = ob_get_contents(); //获取输入缓存流
ob_end_clean(); // 关闭缓存
if(file_exists($filename)){ //删除物理图片
unlink($filename);
}
//返回信息
return response($content,200,['Content-Length'=>strlen($content)])->contentType('image/png');
//输出的话是信息流,也就是图片
}