溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP怎么實(shí)現(xiàn)圖片合并

發(fā)布時(shí)間:2023-04-19 11:12:45 來源:億速云 閱讀:73 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“PHP怎么實(shí)現(xiàn)圖片合并”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

準(zhǔn)備工作

1、需要海報(bào)的底圖

2、小程序碼的圖片 

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

完整過程

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

第二步:縮放小程序碼的圖片大小  (如果尺寸符合海報(bào)大小可省略) 280-1280px

第三步:將縮放后的小程序圖片合成到背景圖片

第四步:合成文字信息

第一步:生成小程序碼圖片 (我使用的場(chǎng)景是無限制小程序碼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)亂碼?

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

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

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

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

設(shè)置看看

“PHP怎么實(shí)現(xiàn)圖片合并”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI