模糊缩小400x400至300x300

时间:2014-07-10 09:16:56

标签: php image-resizing image-compression

好的,这就是问题,400x400 jpeg看起来很好,质量很好。然而,当尺寸调整到300x300时,看起来它被拉开并被一个2岁的人重新组合在一起。

这是脚本

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

}

2 个答案:

答案 0 :(得分:0)

不是答案,只是帮忙,我不能把照片放在评论中......

如果您想知道是否值得安装和使用ImageMagick,我已经为您完成了以下工作:

convert in.jpg -resize 300x300 out.jpg

enter image description here

答案 1 :(得分: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