2014-07-10 151 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 –