2014-07-10 8 views
2

좋아, 여기에 문제가있다. 400x400 jpeg는 품질이 훌륭해 보인다. 그러나 300x300 크기로 조정하면 2 인치짜리 떼어 놓은 것처럼 보입니다. 여기 흐림 축소 400x400에서 300x300까지

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

이미지 클래스

입니다 다음 스크립트를 호출하는 스크립트의

$image->resize(300,300); 

입니다

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

@ 존 어떻게 제목을 것이라고 – RussellHarrower

+1

가능한 복제본 [php imagecopyresampled 저품질] (http://stackoverflow.com/questions/2345605/php-imagecopyresampled-poor-quality) – Theolodis

답변

0

이 기능을 테스트하려면 I 클래스의 나머지 부분을 구현했다. 이것은 나를 위해 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 –