参考链接:https://blog.csdn.net/dream_dt/article/details/79667109
依赖库文件 phpqrcode.php
代码逻辑:
1.生成一张url相关的 二维码 QR
2.把log图片跟QR合并成一个带logo的二维码 last
3.把带logo的的二维码跟 活动图片合成为一张图 保存到本地 返回路径
4.图片中生成文字
5.把合成的图片压缩
public function index(){
$uid = $this->auth->id;
$appId = $GLOBALS['app_id'];
$code = db("user_" . $appId)->field('invite_code,jointime')->where("id", $uid)->find();
//用户根据分享邀请码和分享海报背景图合并
$img = explode(",", 多张图片分割为数组);
$url = '二维码跳转链接';
$x = "293";
$y = "1031";
foreach ($img as $v) {
$template = "http://" . $serverName . $v . "";
$fileName = md5(basename($template) . $url);
$time = date("Ym", $code['jointime']);
$file = '../public/uploads/' . $appId . '/invite/' . $time . '/' . $uid . '_' . $fileName . '.jpg'; //压缩后图片
$fileSrc = "http://" . $serverName . substr($file, 9);
if (file_exists($file)) { //文件存在返回图片路径
$time = time();
$img_time = filemtime($file);
if ($time - $img_time < 86400) {
$list['image'][] = $fileSrc;
} else {
unlink($file);
$resImg = $this->getActivityImg($template, $url, $x, $y, $code['invite_code'], $code['jointime'], $uid);
$list['image'][] = "http://" . $serverName . substr($resImg, 9);
}
} else { //在线生成图片
$resImg = $this->getActivityImg($template, $url, $x, $y, $code['invite_code'], $code['jointime'], $uid);
$list['image'][] = "http://" . $serverName . substr($resImg, 9);
}
}
if (empty($list)) {
$this->error('查询数据为空', []);
} else {
$this->success('数据返回成功', $list);
}
}
//参数 活动模板图片,二维码url,模板内二维码的位置
private function getActivityImg($template, $url, $x, $y, $code, $jointime, $uid) {
$time = date("Ym", $jointime);
$file = $GLOBALS['app_id'];
$md5 = md5(basename($template) . $url);
//创建文件夹
$dir = iconv("UTF-8", "GBK", "../public/uploads/" . $file . "/invite/" . $time . "");
if (!file_exists($dir)) {
mk_dir($dir, 0777, true);
}
//引入二维码类
new \qrstr();
//$QR = "../public/uploads/$file/base.png";
$QR = "../public/uploads/$file/invite/$time/" . $uid . "_" . $md5 . "_base.png";
$errorCorrectionLevel = 'Q'; //防错等级
$matrixPointSize = 5; //二维码大小
//生成二维码
//参数内容:二维码储存内容,生成存储,防错等级,二维码大小,白边大小
\QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);
//合成带logo的二维码图片跟 模板图片--------------start
$path_1 = $template;
$path_2 = $QR;
$image_1 = imagecreatefrompng($path_1);
$image_2 = imagecreatefrompng($path_2);
$image_3 = imageCreatetruecolor(imagesx($image_1), imagesy($image_1));
$color = imagecolorallocate($image_3, 200, 200, 200);
//在图片上加文字
$font_file = ROOT_PATH . "public/assets/fonts/msyh.ttc"; //字体文件
//$font_color_1 = ImageColorAllocate($image_1, 100, 100, 100);
$font_color_1 = ImageColorAllocate($image_1, 255, 255, 255);
imagettftext($image_1, 20, 0, 306, 1000, $font_color_1, $font_file, "邀请码:" . $code); //邀请码
imageline($image_1, 500, 500, 500, 500, $font_color_1);
//在图片上加文字end
imagefill($image_3, 0, 0, $color);
imageColorTransparent($image_3, $color);
imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
imagecopymerge($image_3, $image_2, $x, $y, 0, 0, imagesx($image_2), imagesy($image_2), 100);
//合成带logo的二维码图片跟 模板图片--------------end
//输出到本地文件夹
$fileName = md5(basename($template) . $url);
//$EchoPath = '../public/uploads/' . $file . '/' . $fileName . '.png';
$source = '../public/uploads/' . $file . '/invite/' . $time . '/' . $uid . '_' . $fileName . '.png'; //原图
$EchoPath = '../public/uploads/' . $file . '/invite/' . $time . '/' . $uid . '_' . $fileName . '.jpg'; //压缩后图片
imagepng($image_3, $source);
//imagepng($image_3, $EchoPath);
//$percent = 1;
//$image = (new imgcompress($source,$percent))->compressImg($EchoPath);
$this->handlePic($source);
imagedestroy($image_3);
unlink($source);
unlink($QR);
//返回生成的路径
return $EchoPath;
}
/**
* 图片压缩
* @param $srcPathName
*/
public function handlePic($srcPathName)
{
$srcFile = $srcPathName;
$srcFileExt = strtolower(trim(substr(strrchr($srcFile, '.'), 1)));
if ($srcFileExt == 'png') {
$dstFile = str_replace('.png', '.jpg', $srcPathName);
$photoSize = GetImageSize($srcFile);
$pw = $photoSize[0];
$ph = $photoSize[1];
$dstImage = ImageCreateTrueColor($pw, $ph);
imagecolorallocate($dstImage, 255, 255, 255);
//读取图片
$srcImage = ImageCreateFromPNG($srcFile);
//合拼图片
imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $pw, $ph, $pw, $ph);
imagejpeg($dstImage, $dstFile, 70);
imagedestroy($srcImage);
}
}