生成商品海報,附帶小程式二維碼

yuzifei發表於2021-05-12

話不多說,直接上程式碼


        //商品資訊
        $goods = GoodsModel::info(['id'=>$param['goods_id']]);

        //小程式資訊
        $appletsInfo = AppletsModel::findByUid($param['applets_id']);

        //使用者資訊
        $userInfo = UserModel::info(['id'=>$param['user_id']]);

        //小程式配置
        $config = [

            'app_id' => $appletsInfo['appid'],

            'secret' => $appletsInfo['appsecret'],

            // 下面為可選項

            // 指定 API 呼叫返回結果的型別:array(default)/collection/object/raw/自定義類名

            'response_type' => 'array',

            'log' => [

                'level' => 'debug',

                'file' => __DIR__.'/wechat.log',

            ],

        ];

        $app = Factory::miniProgram($config);

        // 獲取小程式二維碼
        $response = $app->app_code->getUnlimit(round(0000,9999));

        // 儲存小程式碼到檔案
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {

            if(!\file_exists('storage/qrcode')){

                mkdir('storage/qrcode');

            }

            $filename = $response->save('storage/qrcode');

        }

        //生成白色畫布
        $img = imagecreatetruecolor(600,900);
        $color =imagecolorallocate($img,255,255,255);
        imagefill($img,0,0,$color);
        imagepng($img,'haha.png');

        $path_1 = BASE_PATH.'storage/qrcode//'.$filename;
        $path_2 = BASE_PATH.'/'.'haha.png';

        //建立圖片的例項
        switch (\substr($path_1,-3)) {
            case "png":
                $dst = imagecreatefrompng($path_1);
                break;

            case "jpg":
                $dst = imagecreatefromjpeg($path_1);
                break;

            case "jpeg":
                $dst = imagecreatefromjpeg($path_1);
                break;

            case "gif":
                $dst = imagecreatefromgif($path_1);
                break;

        }

        //建立圖片的例項
        switch (\substr($path_2,-3)) {
            case "png":
                $src = imagecreatefrompng($path_2);
                break;

            case "jpg":
                $src = imagecreatefromjpeg($path_2);
                break;

            case "jpeg":
                $src = imagecreatefromjpeg($path_2);
                break;

            case "gif":
                $src = imagecreatefromgif($path_2);
                break;

        }

        //獲取伺服器商品圖片
        $thumb = imagecreatefromstring(file_get_contents($goods['thumb']));
        switch ($thumb) {
            case "png":
                $thumb = imagesy(imagecreatefrompng($goods['thumb']));
                break;

            case "jpg":
                $thumb = imagesy(imagecreatefromjpeg($goods['thumb']));
                break;

            case "jpeg":
                $thumb = imagesy(imagecreatefromjpeg($goods['thumb']));
                break;

            case "gif":
                $thumb = imagesy(imagecreatefromgif($goods['thumb']));
                break;

        }

        //合成小程式二維碼
        imagecopyresampled($src, $dst, 400, 685, 0, 0, 160, 160, imagesx($dst), imagesy($dst));

        //合成商品圖片
        imagecopyresampled($src, $thumb, 110, 240, 0, 0, 400, 400, imagesx($thumb), imagesy($thumb));

        $width   = imagesx($src);//獲取圖片寬
        $height   = imagesy($src);//獲取圖片高

        $colors =imagecolorallocate($img,0,0,0);
        $font = BASE_PATH."simfang_ttf/simfang.ttf";//載入字型ttf
        $font_size = BASE_PATH."simfang_ttf/simhei.ttf";//載入字型ttf

        $fontBox = imagettfbbox(25, 0, $font, $appletsInfo['shop_name']);//獲取文字所需的尺寸大小
        $fontBox_title = imagettfbbox(25, 0, $font, $appletsInfo['shop_name']);//獲取文字所需的尺寸大小

        //新增文字
        imagettftext($src, 25, 0, ceil(($width - $fontBox[2]) / 2), ceil(($height - $fontBox[1] - $fontBox[7]) / 7), $colors, $font_size, $appletsInfo['shop_name']);
        imagettftext($src, 25, 0, 50, 750, $colors, $font, '¥'.$goods['selling_price']);
        imagettftext($src, 15, 0, ceil(($width - $fontBox_title[2]) / 2), ceil(($height - $fontBox_title[1] - $fontBox_title[7]) / 5.5), $colors, $font, '來自:'.$userInfo['nickname'].'的推薦');

        //把商品標題拆分成陣列
        for ($i=0; $i < mb_strlen($goods['title']); $i++) { 
            $letter[] = mb_substr($goods['title'],$i,1);
        }

        $content = '';
        foreach ($letter as $l) {
            $test_str = $content." ".$l;
            $fontBox = imagettfbbox(15, 0, $font, $test_str);
            // 判斷拼接後的字串是否超過預設的寬度
            if (($fontBox[2] > 220) && ($content !== "")) {
                $content .= "\n";
            }

            //組裝成新的文字
            $content .= $l;
        }

        imagettftext($src, 20, 0, 50, 780, $colors, $font_size, $content);//新增文字

        //生成圖片
        imagepng($src,'test2.png');

效果如下:

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章