在OpenCart中删除图像调整比例

6

我已经搜索了很多地方,但没有找到如何在OpenCart中取消图像调整大小的方法。

我需要它进行调整大小,但不保持比例。我希望图像就像我设置的那样。

以下是在system/library/image.php中的调整大小代码。

public function resize($width = 0, $height = 0) {
        if (!$this->info['width'] || !$this->info['height']) {
            return;
        }

        $xpos = 0;
        $ypos = 0;

        $scale = min($width / $this->info['width'], $height / $this->info['height']);

        if ($scale == 1) {
            return;
        }

        $new_width = (int)($this->info['width'] * $scale);
        $new_height = (int)($this->info['height'] * $scale);            
        $xpos = (int)(($width - $new_width) / 2);
        $ypos = (int)(($height - $new_height) / 2);

        $image_old = $this->image;
        $this->image = imagecreatetruecolor($width, $height);

        if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {     
            imagealphablending($this->image, false);
            imagesavealpha($this->image, true);
            $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
            imagecolortransparent($this->image, $background);
        } else {
            $background = imagecolorallocate($this->image, 255, 255, 255);
        }

        imagefilledrectangle($this->image, 0, 0, $width, $height, $background);

        imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
        imagedestroy($image_old);

        $this->info['width']  = $width;
        $this->info['height'] = $height;
    }

我应该移除代码中的哪部分,才能使图片在调整大小时不保持其比例?

1个回答

19
首先,我会将默认的调整大小工具留下,因为在某些情况下它可能很有用。我添加了两个函数来调整图像大小。
一个函数是裁剪图像以适应管理设置的大小。现在添加了空白区域。这对产品列表非常有用。第二个函数是将图像调整大小,使最大尺寸设置为管理设置的最大尺寸。它将按比例缩放。
新文件已发布在OpenCart论坛主题中
我将这两个额外的函数命名为cropsizeonesize 。您只需在控制器中找到使用的调整大小函数并进行调整即可:
'thumb' => $this->model_tool_image
                ->resize($image, 
                         $this->config->get('config_image_category_width'), 
                         $this->config->get('config_image_category_height')));

到:

'thumb' => $this->model_tool_image
                ->cropsize($image, 
                           $this->config->get('config_image_category_width'), 
                           $this->config->get('config_image_category_height')));

onesize函数只需要一个参数,所以取决于您,但您可以使用类似以下内容的东西:

'popup' => $this->model_tool_image
                ->onesize($result['image'], 
                          $this->config->get('config_image_popup_width'))

我希望这可以帮助您获得更好的图像。

这对我非常有效(OpenCart 1.5.2.1)。感谢您发布此代码。 - andrew
1
谢谢,它可以工作了,只是我已经在model/tool/image.php中更改了最后的if else语句,因为我认为OpenCart的新版本需要这样做。我使用了"return $this->config->get('config_ssl') . 'image/' . $new_image;"代替"return HTTPS_IMAGE . $new_image;",然后它就可以工作了。 - nikoloza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接