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

话不多说,直接上代码


        //商品信息
        $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 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

害怕 :flushed: , intervention/image 满足不了你吗!

3年前 评论
草莓熊 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!