欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2023年03月28日 08:57:38   作者:鮮花滿月樓  
這篇文章主要為大家詳細介紹了如何利用PHP實現(xiàn)圖片合并的效果,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下

業(yè)務需求

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

最終效果

準備工作

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 需要設置為 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);
        //復制源文件
        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);
    }

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

/**
     * 圖片合并
     * 將源圖片覆蓋到目標圖片上
     * @param string $dstPath 目標圖片路徑 背景圖
     * @param string $srcPath 源圖片路徑   內(nèi)容圖
     * @param int $dstX 源圖片覆蓋到目標的X軸坐標
     * @param int $dstY 源圖片覆蓋到目標的Y軸坐標
     * @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);
        //將水印圖片復制到目標圖片上,最后個參數(shù)50是設置透明度,這里實現(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 目標圖片
     * @param $fontPath string 字體路徑
     * @param $fontSize string 字體大小
     * @param $text string 文字內(nèi)容
     * @param $dstY string 文字Y坐標值
     * @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://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文字合并的時候出現(xiàn)亂碼?

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

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

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

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

設置看看

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

相關(guān)文章

  • CodeIgniter刪除和設置Cookie的方法

    CodeIgniter刪除和設置Cookie的方法

    這篇文章主要介紹了CodeIgniter刪除和設置Cookie的方法,涉及CodeIgniter操作cookie的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • thinkphp5框架前后端分離項目實現(xiàn)分頁功能的方法分析

    thinkphp5框架前后端分離項目實現(xiàn)分頁功能的方法分析

    這篇文章主要介紹了thinkphp5框架前后端分離項目實現(xiàn)分頁功能的方法,結(jié)合實例形式分析了thinkPHP5前后端分離項目的分頁功能常見實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下
    2019-10-10
  • 在Yii2中使用Pjax導致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析

    在Yii2中使用Pjax導致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析

    這篇文章主要介紹了在Yii2中使用Pjax導致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • Linux+Nginx+MySQL下配置論壇程序Discuz的基本教程

    Linux+Nginx+MySQL下配置論壇程序Discuz的基本教程

    這篇文章主要介紹了Linux+Nginx+MySQL下配置論壇程序Discuz的基本教程,Discuz是用PHP寫成的國內(nèi)最流行的論壇軟件,文中默認前提是已經(jīng)構(gòu)建好了PHP環(huán)境,需要的朋友可以參考下
    2015-12-12
  • PHP中使用extract函數(shù)

    PHP中使用extract函數(shù)

    這篇文章主要介紹了PHP中使用extract函數(shù),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • php微信開發(fā)之上傳臨時素材

    php微信開發(fā)之上傳臨時素材

    這篇文章主要為大家詳細介紹了PHP微信開發(fā)之簡單實現(xiàn)上傳臨時素材的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 淺談php中的訪問修飾符private、protected、public的作用范圍

    淺談php中的訪問修飾符private、protected、public的作用范圍

    下面小編就為大家?guī)硪黄獪\談php中的訪問修飾符private、protected、public的作用范圍。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • 深入理解php printf() 輸出格式化的字符串

    深入理解php printf() 輸出格式化的字符串

    下面小編就為大家?guī)硪黄钊肜斫鈖hp printf() 輸出格式化的字符串。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • php中的array_filter()函數(shù)的使用

    php中的array_filter()函數(shù)的使用

    php中的array_filter()函數(shù)用于篩選數(shù)組中的元素,并返回一個新的數(shù)組,新數(shù)組的元素是所有返回值為true的原數(shù)組元素,本文給大家介紹php中的array_filter()函數(shù)的使用,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • YII Framework框架教程之緩存用法詳解

    YII Framework框架教程之緩存用法詳解

    這篇文章主要介紹了YII Framework框架教程之緩存用法,結(jié)合實例形式詳細分析了Yii框架緩存的結(jié)構(gòu),原理,使用方法與相關(guān)注意事項,需要的朋友可以參考下
    2016-03-03

最新評論