国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

PHP實現(xiàn)圖片合并的示例詳解

瀏覽:165日期:2022-06-11 08:13:35
目錄
  • 業(yè)務(wù)需求
  • 最終效果
  • 準(zhǔn)備工作
  • 完整過程
  • 常見的問題

業(yè)務(wù)需求

我們需要一個微信小程序碼,但是是需要提供給別人掃碼的但是只有一個純粹的小程序碼是不好看的,所以需要推廣的海報圖片。再結(jié)合文字

最終效果

準(zhǔn)備工作

1、需要海報的底圖

2、小程序碼的圖片

代碼部分結(jié)合YII2但不影響使用

完整過程

第一步:生成小程序碼圖片

第二步:縮放小程序碼的圖片大小 (如果尺寸符合海報大小可省略) 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//超時時間    ));$context = stream_context_create($options);return file_get_contents($url, false, $context);    }    //二進制轉(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)建圖片的實例$dst = imagecreatefromstring(file_get_contents($dstPath));$src = imagecreatefromstring(file_get_contents($srcPath));//獲取水印圖片的寬高list($src_w, $src_h) = getimagesize($srcPath);//將水印圖片復(fù)制到目標(biāo)圖片上,最后個參數(shù)50是設(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://GIFif (!$filename) {    header("Content-Type: image/gif");    imagegif($dst);} else {    imagegif($dst, $filename);}break;    case 2://JPGif (!$filename) {    header("Content-Type: image/jpeg");    imagejpeg($dst);} else {    imagejpeg($dst, $filename);}break;    case 3://PNGif (!$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)建圖片的實例$dst = imagecreatefromstring(file_get_contents($dstPath));//打上文字$fontColor = imagecolorallocate($dst, 255, 255, 255);//字體顏色$width = imagesx($dst);$height = imagesy($dst);$fontBox = imagettfbbox($fontSize, 0, $fontPath, $text);//文字水平居中實質(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://GIFif (!$filename) {    header("Content-Type: image/gif");    imagegif($dst);} else {    imagegif($dst, $filename);}break;    case 2://JPGif (!$filename) {    header("Content-Type: image/jpeg");    imagejpeg($dst);} else {    imagejpeg($dst, $filename);}break;    case 3://PNGif (!$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文字合并的時候出現(xiàn)亂碼?

第一檢測一下字體是否是正常tff字體 如果不知道去C://windows/Fonts 隨便找一個 微軟雅黑都行

2、英文阿拉布數(shù)字正常 中文亂碼

$text = mb_convert_encoding("呵呵呵","UTF-8","GBK");

$text = mb_convert_encoding("呵呵呵","html-entities","UTF-8");

設(shè)置看看

到此這篇關(guān)于PHP實現(xiàn)圖片合并的示例詳解的文章就介紹到這了,更多相關(guān)PHP圖片合并內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: PHP
主站蜘蛛池模板: 91在线免费公开视频 | 中文字幕精品视频 | 一级片免费网址 | 欧美成人h精品网站 | 国产精品色午夜视频免费看 | 欧美整片在线观看 | 久色网址 | 亚洲毛片免费视频 | 黄 色 成 年人网站 黄 色 免费网 站 成 人 | 日韩在线视频观看 | 日韩精品欧美国产精品亚 | 欧美日韩视频在线第一区二区三区 | 久久精品国产99国产精品亚洲 | 国产一级片毛片 | 欧美一级欧美三级在线观看 | 成人性色生活影片 | 伊人爱爱网| 久久久久国产精品免费免费 | 美女张开腿让我桶 | 三级黄网站 | 欧美高清视频手机在在线 | 成年女人看片免费视频播放器 | 高清国产一级精品毛片基地 | 毛片一级在线观看 | avav男人天堂 | 亚洲天堂久久久 | 国内精品小视频 | 久草亚洲视频 | 欧美三级观看 | 97视频在线观看免费视频 | 一级特级aaa毛片 | 欧美一级毛片不卡免费观看 | 另类专区亚洲 | 国产精品国三级国产aⅴ | 五月天激激婷婷大综合蜜芽 | 农村寡妇一级毛片免费播放 | 成人五级毛片免费播放 | 日韩a级 | 精品国产美女福到在线不卡f | 91精品国 | 国产精品亚洲一区二区三区在线观看 |