GD 图像模糊 Opencart

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

我在 Opencart 中进行 GD 图像处理时遇到了这个问题,它在调整大小后会创建非常糟糕的模糊图像。到目前为止我所尝试的一切都没有帮助。 下面是 image.php 的代码

<?php
class Image {
    private $file;
    private $image;
    private $info;

    public function __construct($file) {
        if (file_exists($file)) {
            $this->file = $file;

            $info = getimagesize($file);

            $this->info = array(
                'width'  => $info[0],
                'height' => $info[1],
                'bits'   => $info['bits'],
                'mime'   => $info['mime']
            );

            $this->image = $this->create($file);
        } else {
            exit('Error: Could not load image ' . $file . '!');
        }
    }

    private function create($image) {
        $mime = $this->info['mime'];

        if ($mime == 'image/gif') {
            return imagecreatefromgif($image);
        } elseif ($mime == 'image/png') {
            return imagecreatefrompng($image);
        } elseif ($mime == 'image/jpeg') {
            return imagecreatefromjpeg($image);
        }
    }   

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

/**
*   
*   @param width 
*   @param height
*   @param default char [default, w, h]
*                  default = scale with white space, 
*                  w = fill according to width, 
*                  h = fill according to height
    *   
    */
    public function resize($width = 0, $height = 0, $default = '') {
        if (!$this->info['width'] || !$this->info['height']) {
            return;
        }

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

        $scale_w = $width / $this->info['width'];
        $scale_h = $height / $this->info['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->info['mime'] != 'image/png')
 {
            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;
    }

    public function watermark($file, $position = 'bottomright') {
        $watermark = $this->create($file);

        $watermark_width = imagesx($watermark);
        $watermark_height = imagesy($watermark);

        switch($position) {
            case 'topleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = 0;
                break;
            case 'topright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = 0;
                break;
            case 'bottomleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
            case 'bottomright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
        }

        imagecopy($this->image, $watermark, 
$watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);

        imagedestroy($watermark);
    }

    public function crop($top_x, $top_y, $bottom_x, $bottom_y) {
        $image_old = $this->image;
        $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);

        imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, 
$this->info['width'], $this->info['height']);
        imagedestroy($image_old);

        $this->info['width'] = $bottom_x - $top_x;
        $this->info['height'] = $bottom_y - $top_y;
    }

    public function rotate($degree, $color = 'FFFFFF') {
        $rgb = $this->html2rgb($color);

        $this->image = imagerotate($this->image, $degree,
 imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));

        $this->info['width'] = imagesx($this->image);
        $this->info['height'] = imagesy($this->image);
    }

    private function filter($filter) {
        imagefilter($this->image, $filter);
    }

    private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') {
        $rgb = $this->html2rgb($color);

        imagestring($this->image, $size, $x, $y, $text, 
imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
    }

    private function merge($file, $x = 0, $y = 0, $opacity = 100) {
        $merge = $this->create($file);

        $merge_width = imagesx($image);
        $merge_height = imagesy($image);

        imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, 
$merge_height, $opacity);
    }

    private function html2rgb($color) {
        if ($color[0] == '#') {
            $color = substr($color, 1);
        }

        if (strlen($color) == 6) {
            list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3],
 $color[4] . $color[5]);   
        } elseif (strlen($color) == 3) {
            list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1],
 $color[2] . $color[2]);    
        } else {
            return false;
        }

        $r = hexdec($r); 
        $g = hexdec($g); 
        $b = hexdec($b);    

        return array($r, $g, $b);
    }   
}
?>

正如您所看到的,质量已经设置为 100 ,所以这没有帮助。 我尝试用 resample 替换 resize - 但这没有产生可见的结果。 然而,我发现了这个建议(下面的代码)来锐化图像,不幸的是我不知道如何以及在哪里使用它。特别是因为原始代码处理多种图像类型。请帮忙把它放在正确的地方。

 {
$matrix = array(
  array(-1, -1, -1),
  array(-1, 16, -1),
  array(-1, -1, -1),
);

$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0; 
imageconvolution($image, $matrix, $divisor, $offset);

return $image;
}

此外,如果您有其他建议来改进此代码,我们将不胜感激!我认为这适用于整个 Opencart 社区,因为这个问题已经讨论过很多次了,但目前还没有发布可行的解决方案。

php image opencart gd blurry
1个回答
0
投票

quality
参数仅适用于
.jpeg
图像。 要锐化图像,您可以在
imageconvolution()
函数中应用
resize()
代码。

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

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

    $scale_w = $width / $this->info['width'];
    $scale_h = $height / $this->info['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->info['mime'] != 'image/png') {
        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']);

    //Image Sharpening Code

     $matrix = array(
        array(0.0, -1.0, 0.0),
        array(-1.0, 5.0, -1.0),
        array(0.0, -1.0, 0.0)
     );

     $divisor = array_sum(array_map('array_sum', $matrix));
     $offset = 0;
     imageconvolution($this->image, $matrix, $divisor, $offset);

    // End Image Sharpening Code

    imagedestroy($image_old);

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

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.