weixin_41127103 2024-01-05 23:08 采纳率: 0%
浏览 13
已结题

(标签-小程序|关键词-图例)

#网站资源推广生成小程序二维码推广海报,原封面图片被拉伸变形,寻找猿员优化源码,让图片自适应变化环境。
状况图例:

img

img

源码如下:(优化部分请用红框区别)

    /**
     * 生成推广海报
     */
    public function posterUrl()
    {
        if ($this->request->isAjax()) {
            $type = $this->request->param('type');
            $path = 'uploads/poster';
            $filename = 'spread_agent_' . $type . '_' . $this->admin_uid . '.png';
            #获取自定义海报数据
            $setting = [
                'url' => $this->request->domain() . '/static/common/images/poster_agent.jpg',
                'bom_text' => '现在永远是好的创业时机',
                'bom_text_size' => '16',
                'bom_text_color' => 'rgb(0,0,0)'
            ];
            $config = PosterConfig::where(['admin_id' => $this->admin_uid, 'type' => 3])->find();
            if (!empty($config)) {
                $setting = json_decode($config->setting, true);
            }
            $setting['bom_text_color'] = coverToRGB($setting['bom_text_color']);
            $width = 640;
            $height = 1138;
            $poster = new PosterMaker($width, $height, [255, 255, 255]);
            
                                                               // 获取图片的实际尺寸
                    list($originalWidth, $originalHeight) = getimagesize($setting['url']);
                    $scaleWidth = $width / $originalWidth;
                    $scaleHeight = $height / $originalHeight;
                    $scale = min($scaleWidth, $scaleHeight); // 选择一个更小的比例,以确保图片完全适应海报
                    
                    $newWidth = $originalWidth * $scale; // 新宽度
                    $newHeight = $originalHeight * $scale; // 新高度
                    
                    // 计算图片在海报上的位置
                    $xPos = ($width - $newWidth) / 2;
                    $yPos = ($height - $newHeight) / 2;
            
            $poster->addImg($setting['url'], [0, 0], [640, 1138], 0);
            switch ($type) {
                case 'wechat':
                    list($result, $qrcode) = $this->qrcodeUrl($type);
                    if (!$result) {
                        return callback(404, $qrcode);
                    }
                    $poster->addImg($qrcode, [(($width / 2) - (280 / 2)), $height * 0.67], [280, 300]);
                    break;
                case 'h5':
                    $text_link = config('setting.account_domain') . '/#/pages/share/jump?fid=' . $this->admin_uid;
                    $poster->addQrCode($text_link, [(($width / 2) - (280 / 2)), $height * 0.67], [300, 300]);
                    break;
                case 'douyin':
                    list($result, $qrcode) = $this->qrcodeUrl($type);
                    if (!$result) {
                        return callback(404, $qrcode);
                    }
                    $poster->addImg($qrcode, [(($width / 2) - (280 / 2)), $height * 0.67], [300, 300]);
                    break;
            }
            $poster->addText($setting['bom_text'], $setting['bom_text_size'] * 1.5, [0, $height * 0.98], $setting['bom_text_color']);
            $content = $poster->render($filename, 1); // 保持为图片
            $storage_type = config('setting.upload_storage');
            if ($storage_type == 'local') {
                list($res, $info) = $this->savePoster($content, $path, $filename, true);
                if (!$res) {
                    return callback(404, $info);
                }
                return callback(200, 'success', '', $this->request->domain() . '/' . $path . '/' . $info);
            } elseif ($storage_type == 'aliyun') {
                $oss = new Alioss();
                $result = $oss->pudata($filename, $content, $path);
                if ($result['status'] !== 200) {
                    return callback(404, $result['msg']);
                }
                return callback(200, 'success', '', $result['data']['url']);
            } elseif ($storage_type == 'qcloud') {
                $oss = new Qcloud();
                $result = $oss->pudata($filename, $content, $path);
                if ($result['status'] !== 200) {
                    return callback(404, $result['msg']);
                }
                return callback(200, 'success', '', $result['data']['url']);
            } elseif ($storage_type == 'qiniu') {
                $qiniu = new Qiniu;
                $url = $qiniu->uploadData($content, $path . '/' . $filename);
                if (!$url) {
                    return callback(404, '生成失败');
                }

  • 写回答

14条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-01-05 23:09
    关注
    获得0.75元问题酬金

    引自免费微信小程序:皆我百晓生

    问题描述:在小程序中生成推广海报时,原封面图片会被拉伸变形,希望找到优化源码的方法,使图片能够自适应变化环境。

    解决方案: 首先,我会分析代码中影响图片变形的部分,并进行优化。根据提供的源码,我观察到以下几个关键点:

    1. 获取图片的实际尺寸: 在代码中,使用getimagesize($setting['url'])来获取图片的实际尺寸。这是一个关键步骤,因为我们需要知道原始图片的宽度和高度,以便在海报上适应调整。

    2. 计算图片缩放比例: 通过比较海报的宽度和高度与原始图片的宽度和高度,选择更小的比例,以确保图片完全适应海报。

    3. 计算图片在海报上的位置: 根据计算出的新的宽度和高度,以及海报的宽度和高度,计算图片在海报上的位置,使其居中。

    基于以上分析,我会对代码进行如下优化:(已用红框区分

        /**
         * 生成推广海报
         */
        public function posterUrl()
        {
            if ($this->request->isAjax()) {
                $type = $this->request->param('type');
                $path = 'uploads/poster';
                $filename = 'spread_agent_' . $type . '_' . $this->admin_uid . '.png';
                #获取自定义海报数据
                $setting = [
                    'url' => $this->request->domain() . '/static/common/images/poster_agent.jpg',
                    'bom_text' => '现在永远是好的创业时机',
                    'bom_text_size' => '16',
                    'bom_text_color' => 'rgb(0,0,0)'
                ];
                $config = PosterConfig::where(['admin_id' => $this->admin_uid, 'type' => 3])->find();
                if (!empty($config)) {
                    $setting = json_decode($config->setting, true);
                }
                $setting['bom_text_color'] = coverToRGB($setting['bom_text_color']);
                $width = 640;
                $height = 1138;
                $poster = new PosterMaker($width, $height, [255, 255, 255]);
                                                                   // 获取图片的实际尺寸
                        list($originalWidth, $originalHeight) = getimagesize($setting['url']);
                        $scaleWidth = $width / $originalWidth;
                        $scaleHeight = $height / $originalHeight;
                        $scale = min($scaleWidth, $scaleHeight); // 选择一个更小的比例,以确保图片完全适应海报
                        
                        $newWidth = $originalWidth * $scale; // 新宽度
                        $newHeight = $originalHeight * $scale; // 新高度
                        
                        // 计算图片在海报上的位置
                        $xPos = ($width - $newWidth) / 2;
                        $yPos = ($height - $newHeight) / 2;
                
                $poster->addImg($setting['url'], [0, 0], [$newWidth, $newHeight], 0); // 使用新尺寸
                
                switch ($type) {
                    case 'wechat':
                        list($result, $qrcode) = $this->qrcodeUrl($type);
                        if (!$result) {
                            return callback(404, $qrcode);
                        }
                        $poster->addImg($qrcode, [(($width / 2) - (280 / 2)), $height * 0.67], [280, 300]);
                        break;
                    case 'h5':
                        $text_link = config('setting.account_domain') . '/#/pages/share/jump?fid=' . $this->admin_uid;
                        $poster->addQrCode($text_link, [(($width / 2) - (280 / 2)), $height * 0.67], [300, 300]);
                        break;
                    case 'douyin':
                        list($result, $qrcode) = $this->qrcodeUrl($type);
                        if (!$result) {
                            return callback(404, $qrcode);
                        }
                        $poster->addImg($qrcode, [(($width / 2) - (280 / 2)), $height * 0.67], [300, 300]);
                        break;
                }
                $poster->addText($setting['bom_text'], $setting['bom_text_size'] * 1.5, [0, $height * 0.98], $setting['bom_text_color']);
                $content = $poster->render($filename, 1); // 保持为图片
                $storage_type = config('setting.upload_storage');
                if ($storage_type == 'local') {
                    list($res, $info) = $this->savePoster($content, $path, $filename, true);
                    if (!$res) {
                        return callback(404, $info);
                    }
                    return callback(200, 'success', '', $this->request->domain() . '/' . $path . '/' . $info);
                } elseif ($storage_type == 'aliyun') {
                    $oss = new Alioss();
                    $result = $oss->pudata($filename, $content, $path);
                    if ($result['status'] !== 200) {
                        return callback(404, $result['msg']);
                    }
                    return callback(200, 'success', '', $result['data']['url']);
                } elseif ($storage_type == 'qcloud') {
                    $oss = new Qcloud();
                    $result = $oss->pudata($filename, $content, $path);
                    if ($result['status'] !== 200) {
                        return callback(404, $result['msg']);
                    }
                    return callback(200, 'success', '', $result['data']['url']);
                } elseif ($storage_type == 'qiniu') {
                    $qiniu = new Qiniu;
                    $url = $qiniu->uploadData($content, $path . '/' . $filename);
                    if (!$url) {
                        return callback(404, '生成失败');
                    }
                    return callback(200, 'success', '', $url);
                }
            }
        }
    

    我对代码的优化部分进行了红框标记。通过计算图片的缩放比例和在海报上的位置,来实现图片的自适应变化环境。希望这个优化方案能够解决你的问题。如果还有其他问题,请随时提问。

    评论

报告相同问题?

问题事件

  • 系统已结题 1月13日
  • 创建了问题 1月5日