php - 删除 OpenCart 中的图像调整比例

标签 php image-resizing opencart

我到处查看如何删除 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;
    }

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

最佳答案

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

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

新文件是posted in an OpenCart forum thread .

我将这两个额外的函数命名为cropsizeonesize。您所要做的就是在 Controller 中找到使用的调整大小功能并进行调整:

'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'))

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

关于php - 删除 OpenCart 中的图像调整比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7081860/

相关文章:

php - OpenCart 3 : system cache cleaning, ocmod 和 Twig 。我对 Controller 进行了更改,但没有反射(reflect)出来

php - 评论者只能在他们的评论中删除评论选项

php - Ajax 更新 Woocommerce 中购物车菜单上的产品数量

php - 如何在 Eclipse 中设置 Zend Debugger?

linux - 调整现有 mediawiki 图像的大小

opencart - opencart中的扩展和模块有什么区别?

php - 通过添加两个表单字段获取结束日期

c++ - 如何在 OpenCV 中将图像调整为特定大小?

javascript - 通过 JS 发送时,PHP 图片上传不调整大小

opencart多商店的SSL设置,一台服务器上的多个域名