2016-05-14 313 views
1

我正在使用opencart 2.1.x opencart的版本,我在顯示圖像時遇到圖像問題。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; 
} 

請幫助我擺脫背景噪音。

回答

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); 
    } 
}