删除 OpenCart 中的图像调整比例

发布于 2024-11-30 00:59:05 字数 1716 浏览 0 评论 0原文

我到处都在寻找如何删除 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;
    }

我可以删除该代码中的哪些内容,以便图像在调整大小时不保持其比例?

I've look everywhere on how I could remove the image resizing in OpenCart but haven't find nothing about that.

I need that it resize but don't keep the ratio. I want the image to be just like I set it.

Here's the resize code in the 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;
    }

What in that code could I remove so the image don't keep it's ratio on resize ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2024-12-07 00:59:05

首先,我会保留默认的调整大小工具,因为在某些情况下它可能会派上用场。我所做的是添加两个函数来调整图像大小。

一项功能可以裁剪图像,使其适合管理员中设置的尺寸。现在添加了空白的白色区域。这对于产品列表来说非常有用。我添加的第二个功能是调整图像大小的函数,因此最大尺寸将设置为管理员中设置的最大尺寸。它将按比例缩放。

新文件发布在 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'))

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

First of all I would leave the default resizing tool as in some circumstances it might come in handy. What I did was add two more functions to resize images.

One function that crops an image so it fits the size set in the admin. Now empty white areas are added. This one is great for product lists. The second I added is a function that resizes an image so the biggest dimension will be set to the max size set in the admin. It will be scaled proportionally.

The new files are posted in an OpenCart forum thread.

I named the two extra functions cropsize and onesize. All you have to do is find the used resize functions in the controller and adjust this:

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

to:

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

The onesize function only needs one parameter so it's up to you but you could use something like this:

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

I hope this will help you getting better images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文