PHP實(shí)現(xiàn)圖片合并的示例詳解
業(yè)務(wù)需求
我們需要一個(gè)微信小程序碼,但是是需要提供給別人掃碼的但是只有一個(gè)純粹的小程序碼是不好看的,所以需要推廣的海報(bào)圖片。再結(jié)合文字


最終效果

準(zhǔn)備工作
1、需要海報(bào)的底圖
2、小程序碼的圖片
代碼部分結(jié)合YII2但不影響使用
完整過程
第一步:生成小程序碼圖片
第二步:縮放小程序碼的圖片大小 (如果尺寸符合海報(bào)大小可省略) 280-1280px
第三步:將縮放后的小程序圖片合成到背景圖片
第四步:合成文字信息
第一步:生成小程序碼圖片 (我使用的場景是無限制小程序碼code地址 三種自行選擇)
//微信小程序 小程序碼
public static function getWeChatSmallProgramCode($scene)
{
$AccessToken = self::getAccessToken();
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $AccessToken;
$postData = [
'scene' => $scene,
'page' => 'pages/index/index',
'width'=>930
];
$postData = json_encode($postData);
$contentData = self::sendPost($url, $postData);
return $contentData; //如果圖片大小符合這開啟base64位圖片地址也可以完成圖片的合并合文字的合并
// return self::base64UrlCode($contentData, 'image/png');
}
protected static function sendPost($url, $post_data)
{
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/json',
//header 需要設(shè)置為 JSON
'content' => $post_data,
'timeout' => 60
//超時(shí)時(shí)間
)
);
$context = stream_context_create($options);
return file_get_contents($url, false, $context);
}
//二進(jìn)制轉(zhuǎn)圖片image/png
public static function base64UrlCode($contents, $mime)
{
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}第二步:縮放小程序碼的圖片大小
/**
* 縮放圖片尺寸
* @param $img_path string 圖片地址
* @param $new_width
* @param $new_height
* @param $new_img_path string 新的圖片地址
*/
public static function picZoom($img_path,$new_width,$new_height,$new_img_path)
{
//獲取尺寸
list($width, $height, $img_type, $attr) = getimagesize($img_path);
$imageinfo = [
'width' => $width,
'height' => $height,
'type' => image_type_to_extension($img_type, false),
'attr' => $attr
];
$fun = "imagecreatefrom" . $imageinfo['type'];
$image = $fun($img_path);
//創(chuàng)建新的幕布
$image_thump = imagecreatetruecolor($new_width, $new_height);
//復(fù)制源文件
imagecopyresampled($image_thump, $image, 0, 0, 0, 0, $new_width, $new_height, $imageinfo['width'], $imageinfo['height']);
imagedestroy($image);
$image = $image_thump;
$func = 'image' . $imageinfo['type'];
$func($image, $new_img_path);
}第三步:將縮放后的小程序圖片合成到背景圖片
/**
* 圖片合并
* 將源圖片覆蓋到目標(biāo)圖片上
* @param string $dstPath 目標(biāo)圖片路徑 背景圖
* @param string $srcPath 源圖片路徑 內(nèi)容圖
* @param int $dstX 源圖片覆蓋到目標(biāo)的X軸坐標(biāo)
* @param int $dstY 源圖片覆蓋到目標(biāo)的Y軸坐標(biāo)
* @param int $srcX
* @param int $srcY
* @param int $pct 透明度
* @param string $filename 輸出的文件名,為空則直接在瀏覽器上輸出顯示
* @return string $filename 合并后的文件名
*/
public static function picMerge($dstPath, $srcPath, $dstX = 0, $dstY = 0, $srcX = 0, $srcY = 0, $pct = 100, $filename = '')
{
//創(chuàng)建圖片的實(shí)例
$dst = imagecreatefromstring(file_get_contents($dstPath));
$src = imagecreatefromstring(file_get_contents($srcPath));
//獲取水印圖片的寬高
list($src_w, $src_h) = getimagesize($srcPath);
//將水印圖片復(fù)制到目標(biāo)圖片上,最后個(gè)參數(shù)50是設(shè)置透明度,這里實(shí)現(xiàn)半透明效果
// imagecopymerge($dst, $src, 80, 125, 0, 0, $src_w, $src_h, 100);
imagecopymerge($dst, $src, $dstX, $dstY, $srcX, $srcY, $src_w, $src_h, $pct);
//如果水印圖片本身帶透明色,則使用imagecopy方法
//imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
//輸出圖片
list($dst_w, $dst_h, $dst_type) = getimagesize($dstPath);
switch ($dst_type) {
case 1://GIF
if (!$filename) {
header('Content-Type: image/gif');
imagegif($dst);
} else {
imagegif($dst, $filename);
}
break;
case 2://JPG
if (!$filename) {
header('Content-Type: image/jpeg');
imagejpeg($dst);
} else {
imagejpeg($dst, $filename);
}
break;
case 3://PNG
if (!$filename) {
header('Content-Type: image/png');
imagepng($dst);
} else {
imagepng($dst, $filename);
}
break;
default:
break;
}
imagedestroy($dst);
imagedestroy($src);
}第四步:合成文字信息
/**
* 添加文字到圖片上
* @param $dstPath string 目標(biāo)圖片
* @param $fontPath string 字體路徑
* @param $fontSize string 字體大小
* @param $text string 文字內(nèi)容
* @param $dstY string 文字Y坐標(biāo)值
* @param string $filename 輸出文件名,為空則在瀏覽器上直接輸出顯示
* @return string 返回文件名
*/
public static function addFontToPic($dstPath, $fontPath, $fontSize, $text, $dstY, $filename = '')
{
ob_end_clean();
//創(chuàng)建圖片的實(shí)例
$dst = imagecreatefromstring(file_get_contents($dstPath));
//打上文字
$fontColor = imagecolorallocate($dst, 255, 255, 255);//字體顏色
$width = imagesx($dst);
$height = imagesy($dst);
$fontBox = imagettfbbox($fontSize, 0, $fontPath, $text);//文字水平居中實(shí)質(zhì)
imagettftext($dst, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $dstY, $fontColor, $fontPath, $text);
//輸出圖片
list($dst_w, $dst_h, $dst_type) = getimagesize($dstPath);
switch ($dst_type) {
case 1://GIF
if (!$filename) {
header('Content-Type: image/gif');
imagegif($dst);
} else {
imagegif($dst, $filename);
}
break;
case 2://JPG
if (!$filename) {
header('Content-Type: image/jpeg');
imagejpeg($dst);
} else {
imagejpeg($dst, $filename);
}
break;
case 3://PNG
if (!$filename) {
header('Content-Type: image/png');
imagepng($dst);
} else {
imagepng($dst, $filename);
}
break;
default:
break;
}
imagedestroy($dst);
return $filename;
}外部的調(diào)用
/**
* 根據(jù)店鋪id 和名稱 合成A5 圖片小程序圖片
* @param $shop_id
* @param $shop_name
* @return array
*/
public static function generateWeChatAppletImage($shop_id, $shop_name)
{
//1 生成小程序碼
//2 合成小程序碼到背景圖片
$sceneStr = '?shop_id=' . $shop_id;
$weChatAppImgBaseData = WxTools::getWeChatSmallProgramCode($sceneStr);
$weChatAppImgPath = './weChatAppImg/shop_code_' . $shop_id . '.jpg';
file_put_contents($weChatAppImgPath, $weChatAppImgBaseData);
//合并到背景圖片中
$beiJinImgPath = './weChatAppImg/weChatBJ.jpg';
$mergeImgFile = './weChatAppImg/shop_mini_program' . $shop_id . '.jpg';
GenerateCodeImg::picMerge($beiJinImgPath, $weChatAppImgPath, 408, 714, $srcX = 0, $srcY = 0, $pct = 100, $mergeImgFile);
//3 合成文字
$fontPath = './plus/fonts/SourceHanSansCN-Bold.ttf';
$fontSize = 40;
$dstY = 640;
GenerateCodeImg::addFontToPic($mergeImgFile, $fontPath, $fontSize, $shop_name, $dstY, $mergeImgFile);
$weChatCodeImgUrL = \Yii::$app->request->hostInfo . '/weChatAppImg/shop_code_' . $shop_id . '.jpg';
$weChatAppImgUrl = \Yii::$app->request->hostInfo . '/weChatAppImg/shop_mini_program' . $shop_id . '.jpg';
return [
'weChatCodeImgUrL' => $weChatCodeImgUrL,
'weChatAppImgUrl' => $weChatAppImgUrl,
];
}常見的問題
1文字合并的時(shí)候出現(xiàn)亂碼?
第一檢測一下字體是否是正常tff字體 如果不知道去C://windows/Fonts 隨便找一個(gè) 微軟雅黑都行
2、英文阿拉布數(shù)字正常 中文亂碼
$text = mb_convert_encoding("呵呵呵","UTF-8","GBK");
$text = mb_convert_encoding("呵呵呵","html-entities","UTF-8");
設(shè)置看看
到此這篇關(guān)于PHP實(shí)現(xiàn)圖片合并的示例詳解的文章就介紹到這了,更多相關(guān)PHP圖片合并內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用PHP連接數(shù)據(jù)庫_實(shí)現(xiàn)用戶數(shù)據(jù)的增刪改查的整體操作示例
下面小編就為大家?guī)硪黄褂肞HP連接數(shù)據(jù)庫_實(shí)現(xiàn)用戶數(shù)據(jù)的增刪改查的整體操作示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
PHP substr()函數(shù)參數(shù)解釋及用法講解
substr() 函數(shù)返回字符串的一部分。這篇文章給大家介紹了PHP substr()函數(shù)參數(shù)解釋及用法講解,需要的朋友參考下2017-11-11
PHPWind9.0手動(dòng)屏蔽驗(yàn)證碼解決后臺(tái)關(guān)閉驗(yàn)證碼但是依然顯示的問題
這篇文章主要介紹了PHPWind9.0手動(dòng)屏蔽驗(yàn)證碼解決后臺(tái)關(guān)閉驗(yàn)證碼但是依然顯示的問題的相關(guān)資料,需要的朋友可以參考下2016-08-08
php設(shè)計(jì)模式之中介者模式分析【星際爭霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之中介者模式,結(jié)合具體案例形式分析了星際爭霸游戲中使用PHP中介者模式的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03

