Opencart - 图像调整大小

问题描述 投票:1回答:2

我正在使用opencart 2.1.x版本的opencart,我正面临着显示图像的图像问题。

但图像调整大小功能在背景中添加Noise。它可以在下图中看到::

enter image description here

倾斜笔记本电脑屏幕或桌面屏幕以观察噪音

调整图像大小的功能是:

catalog/model/tool/image.php

public function resize($filename, $width, $height) {
    if (!is_file(DIR_IMAGE . $filename)) {
        return;
}
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$old_image = $filename;
$new_image = 'cache/' . utf8_substr($filename, 0, 
             utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.'.$extension;

list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image);

if ($width_orig != $width || $height_orig != $height) {
    $image = new Image(DIR_IMAGE . $old_image);
    $image->resize($width, $height);
    $image->save(DIR_IMAGE . $new_image);
} else {
    copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image);
}

system/library/image.php

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

    $xpos = 0;
    $ypos = 0;
    $scale = 1;

    $scale_w = $width / $this->width;
    $scale_h = $height / $this->height;

    if ($default == 'w') {
        $scale = $scale_w;
    } elseif ($default == 'h') {
        $scale = $scale_h;
    } else {
        $scale = min($scale_w, $scale_h);
    }

    if ($scale == 1 && $scale_h == $scale_w && $this->mime != 'image/png') {
        return;
    }

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

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

    if ($this->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->width, $this->height);
    imagedestroy($image_old);

    $this->width = $width;
    $this->height = $height;
 }

请协助摆脱背景噪音。

php image resize options opencart2.x
2个回答
1
投票

看起来你的catalog/model/tool/image.php代码片段不完整。

我通过注释函数代码的主要部分并返回初始图像的路径(OpenCart 2.3.0.2)来解决这个问题:

<?php
class ModelToolImage extends Model {
    public function resize($filename, $width, $height) {
        if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != DIR_IMAGE) {
            return;
        }

        /*$extension = pathinfo($filename, PATHINFO_EXTENSION);

        $image_old = $filename;
        $image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;

        if (!is_file(DIR_IMAGE . $image_new) || (filectime(DIR_IMAGE . $image_old) > filectime(DIR_IMAGE . $image_new))) {
            list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old);

            if (!in_array($image_type, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) { 
                return DIR_IMAGE . $image_old;
            }

            $path = '';

            $directories = explode('/', dirname($image_new));

            foreach ($directories as $directory) {
                $path = $path . '/' . $directory;

                if (!is_dir(DIR_IMAGE . $path)) {
                    @mkdir(DIR_IMAGE . $path, 0777);
                }
            }

            if ($width_orig != $width || $height_orig != $height) {
                $image = new Image(DIR_IMAGE . $image_old);
                $image->resize($width, $height);
                $image->save(DIR_IMAGE . $image_new);
            } else {
                copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
            }
        }

        $image_new = str_replace(' ', '%20', $image_new);  // fix bug when attach image on email (gmail.com). it is automatic changing space " " to +

        if ($this->request->server['HTTPS']) {
            return $this->config->get('config_ssl') . 'image/' . $image_new;
        } else {
            return $this->config->get('config_url') . 'image/' . $image_new;
        }*/

        return 'image/' . $filename;
    }
}

0
投票

您是否已尝试将参数中的功能保存为100

public function save($file, $quality = 100) {
    $info = pathinfo($file);

    $extension = strtolower($info['extension']);

    if (is_resource($this->image)) {
        if ($extension == 'jpeg' || $extension == 'jpg') {
            imagejpeg($this->image, $file, $quality);
        } elseif ($extension == 'png') {
            imagepng($this->image, $file);
        } elseif ($extension == 'gif') {
            imagegif($this->image, $file);
        }

        imagedestroy($this->image);
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.