2014-07-10 103 views
2

好的,这里是问题,400x400的jpeg看起来不错,质量很好。但是,当调整到300x300时,它看起来已经被拉开,并被2岁的孩子放在一起。400x400到300x300的模糊缩小尺寸

这里是脚本

$image->resize(300,300); 

它调用下面的脚本

function resize($width,$height) { 
     $new_image = imagecreatetruecolor($width, $height); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    } 

picture uploaded for resizing.

enter image description here

这里是图像类

class SimpleImage { 

    var $image; 
    var $image_type; 

    function load($filename) { 

     $image_info = getimagesize($filename); 
     $this->image_type = $image_info[2]; 
     if($this->image_type == IMAGETYPE_JPEG) { 

     $this->image = imagecreatefromjpeg($filename); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 

     $this->image = imagecreatefromgif($filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 

     $this->image = imagecreatefrompng($filename); 
     } 
    } 

    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=99, $permissions=null) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image,$filename,$compression); 
     } elseif($image_type == IMAGETYPE_GIF) { 
     imagegif($this->image,$filename); 
     } elseif($image_type == IMAGETYPE_PNG) { 
     imagepng($this->image,$filename); 
     } 


     if($permissions != null) { 

     chmod($filename,$permissions); 
     } 
    } 
    function output($image_type=IMAGETYPE_JPEG) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     imagegif($this->image); 
     } elseif($image_type == IMAGETYPE_PNG) { 

     imagepng($this->image); 
     } 
    } 

    function getWidth() { 

     return imagesx($this->image); 
    } 
    function getHeight() { 

     return imagesy($this->image); 
    } 
    function resizeToHeight($height) { 

     $ratio = $height/$this->getHeight(); 
     $width = $this->getWidth() * $ratio; 
     $this->resize($width,$height); 
    } 

    function resize($width,$height) { 
     $new_image = imagecreatetruecolor($width, $height); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    } 

} 
+0

好问题,但我觉得标题可以做得更具体。 – Jon

+0

@Jon如何标题它 – RussellHarrower

+1

可能重复的[php imagecopyresampled质量差](http://stackoverflow.com/questions/2345605/php-imagecopyresampled-poor-quality) – Theolodis

回答

0

要测试的功能,我不得不实现该类的其余部分。这适用于PHP 5.5.14。您的代码包含在此不变:

class image 
{ 
    public $image = null; 

    public function __construct($img) 
    { 
     $this->image = imagecreatefromjpeg($img); 
    } 

    public function resize($width,$height) { 
     $new_image = imagecreatetruecolor($width, $height); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    } 

    public function getWidth() 
    { 
     return imagesx($this->image); 
    } 

    public function getHeight() 
    { 
     return imagesy($this->image); 
    } 
} 

$image = new image('img.jpg'); 

$image->resize(300,300); 

header('Content-Type: image/jpeg'); 
imagejpeg($image->image); 
exit; 

这给了我下面的输出,这是一个有点模糊,但比你的例子要好得多。

enter image description here

+0

我已经添加我的问题的完整代码 – RussellHarrower

+0

我发现我的问题 - 这是事实上,我正在调整图像80x80,然后再次将其升高到300x300我的错误 – RussellHarrower

0

不是一个真正的答案,但只是帮忙,我不能把照片在评论...

在你想知道它是否将是值得安装和使用ImageMagick的情况下,我已经做到了下面为您:

convert in.jpg -resize 300x300 out.jpg 

enter image description here

+0

我如何在PHP中使用这个,ImageMagick是一个Yum安装 - 因为它看起来像你给了我一个终端命令 – RussellHarrower

+0

http://www.php.net/manual/en/imagick.setup.php –