【问题标题】:Upload image without losing quality上传图片不丢失质量
【发布时间】:2017-01-31 23:03:32
【问题描述】:

我使用这个类来上传和调整图像大小。问题是图像质量很差!我需要上传图片而不损失质量!

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=75, $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 resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }

   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $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;   
   }      
}

有人可以帮忙吗?

【问题讨论】:

标签: php


【解决方案1】:

你可以做的一件事是当你调用函数保存时,

当您调用imagejpeg 时,将质量参数从 75 更改为 100

质量是可选的,范围从 0(质量最差,文件较小)到 100(质量最好,文件最大)。默认值为默认的 IJG 质量值(约 75)。

【讨论】:

  • 注意imagepng,因为它的数字来自9 - 0(是的,倒数)。
  • 我认为完美的图像 90,但常规使用 85 可接受的值。
【解决方案2】:

我看到 75 作为 $compression 参数的默认值。您将其作为imagejpeg 的质量参数传递。这可能是造成质量损失的原因。

【讨论】:

    【解决方案3】:

    你保存函数的默认质量值为 75。所以要么在调用它时将其更改为 100,要么将默认值更改为 100。100 是最好的质量。

    【讨论】:

      猜你喜欢
      • 2011-12-31
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多