生成商品海报,附带小程序二维码

Laravel框架
373
0
0
2022-04-13

话不多说,直接上代码

//商品信息
        $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');

效果如下: