參考連接:https://blog.csdn.net/dream_dt/article/details/79667109
依賴庫文件 phpqrcode.php
代碼邏輯:
1.生成一張url相關的 二維碼 QR
2.把log圖片跟QR合併成一個帶logo的二維碼 last
3.把帶logo的的二維碼跟 活動圖片合成爲一張圖 保存到本地 返回路徑
4.圖片中生成文字
5.把合成的圖片壓縮php
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); } }