作者:VEACEN晨k | 来源:互联网 | 2023-08-14 09:15
(1)首先来效果图
/*** 从图片文件创建Image资源* @param $file 图片文件,支持url* @return bool|resource 成功返回图片image资源,失败返回false*/public function createImageFromFile($file){if (preg_match('/http(s)?:\/\//', $file)) {$fileSuffix = $this->getNetworkImgType($file);} else {$fileSuffix = pathinfo($file, PATHINFO_EXTENSION);}if (!$fileSuffix) return false;switch ($fileSuffix) {case 'jpeg':$theImage = @imagecreatefromjpeg($file);break;case 'jpg':$theImage = @imagecreatefromjpeg($file);break;case 'png':$theImage = @imagecreatefrompng($file);break;case 'gif':$theImage = @imagecreatefromgif($file);break;default:$theImage = @imagecreatefromstring(file_get_contents($file));break;}return $theImage;}/*** 分享图片生成* @param $gData 商品数据,array* @param $codeName 二维码图片* @param $fileName string 保存文件名,默认空则直接输入图片*/function createSharePng($gData, $codeName, $fileName = ''){//创建画布$im = imagecreatetruecolor(618, 1000);//填充画布背景色$color = imagecolorallocate($im, 255, 255, 255);imagefill($im, 0, 0, $color);//字体文件$font_file = DI()->config->get('common.share_font');$font_file_bold = DI()->config->get('common.share_font_blod');//设定字体的颜色$font_color_1 = ImageColorAllocate($im, 153, 153, 153);$font_color_2 = ImageColorAllocate($im, 51, 51, 51);$font_color_3 = ImageColorAllocate($im, 129, 129, 129);$font_color_red = ImageColorAllocate($im, 237, 110, 29);$fang_bg_color = ImageColorAllocate($im, 254, 216, 217);//Logo$logo = DI()->config->get('common.share_logo');list($l_w, $l_h) = getimagesize($logo);$logoImg = @imagecreatefrompng($logo);imagecopyresized($im, $logoImg, 274, 28, 0, 0, 70, 70, $l_w, $l_h);//温馨提示imagettftext($im, 14, 0, 100, 130, $font_color_1, $font_file, '温馨提示:喜欢长按图片识别二维码即可前往购买');//商品图片list($g_w, $g_h) = getimagesize($gData['pic']);$goodImg = $this->createImageFromFile($gData['pic']);imagecopyresized($im, $goodImg, 0, 185, 0, 0, 618, 618, $g_w, $g_h);//二维码list($code_w, $code_h) = getimagesize($codeName);$codeImg = $this->createImageFromFile($codeName);imagecopyresized($im, $codeImg, 440, 820, 0, 0, 160, 160, $code_w, $code_h);//商品描述$theTitle = $this->cn_row_substr($gData['title'], 2, 20);imagettftext($im, 14, 0, 48, 845, $font_color_2, $font_file, html_entity_decode($theTitle[1]));imagettftext($im, 14, 0, 48, 875, $font_color_2, $font_file, $theTitle[2]);imagettftext($im, 16, 0, 48, 935, $font_color_2, $font_file, "价格:");imagettftext($im, 18, 0, 100, 935, $font_color_red, $font_file_bold, "¥" . $gData["price"]);//桃子抵扣数量if ($gData['original_price']) {imagettftext($im, 14, 0, 48, 970, $font_color_3, $font_file, "(桃子可抵扣¥" . $gData["original_price"] . ")");}//输出图片if ($fileName) {imagepng($im, $fileName);} else {Header("Content-Type: image/png");imagepng($im);}//释放空间imagedestroy($im);imagedestroy($goodImg);imagedestroy($codeImg);}/*** 获取网络图片类型* @param $url 网络图片url,支持不带后缀名url* @return bool*/public function getNetworkImgType($url){$ch = curl_init(); //初始化curlcurl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URLcurl_setopt($ch, CURLOPT_NOBODY, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时curl_setopt($ch, CURLOPT_TIMEOUT, 3);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持httpscurl_exec($ch);//执行curl会话$http_code = curl_getinfo($ch);//获取curl连接资源句柄信息curl_close($ch);//关闭资源连接if ($http_code['http_code'] == 200) {$theImgType = explode('/', $http_code['content_type']);if ($theImgType[0] == 'image') {return $theImgType[1];} else {return false;}} else {return false;}}