-1

URL から複数の画像のサイズを変更して保存していますが、640x320 フォルダーに保存されている画像が 400kb であるため、これらの画像をさらに圧縮する方法を考えていました。アドバイスをお待ちしております!

PHP のサイズ変更およびサイズ ハンドラー

include("../includes/picture-resize.php");

$image = $_POST['thumbnail'];
$slug = $_POST['slug'];
$images = $_POST['screenshots'];
$list = explode(",", $images);      
$listlength = count($list);

$i = 0;

$image = $_POST['thumbnail'];

$path = parse_url($image, PHP_URL_PATH);

$filename = $slug.'-'.$i;

$extension = pathinfo($path, PATHINFO_EXTENSION);

$file = $filename.'.'.$extension;

file_put_contents('../tmp/' . $file, file_get_contents($image));

$picture = new pic_resize();

$picture->load('../tmp/'.$file);

$picture->resizeToWidth(125);

mkdir('../images/125x125/'.$slug);

$picture->save('../images/125x125/'.$slug.'/'.$file, $picture->image_type);

unlink('../tmp/'.$file);

$thumbnail = $file;

$new_list = array();

mkdir('../images/640x320/'.$slug);

mkdir('../images/310x205/'.$slug);

while($listlength > $i) {

    $path = parse_url($list[$i], PHP_URL_PATH);

    $filename = $slug.'-'.$i;

    $extension = pathinfo($path, PATHINFO_EXTENSION);

    $file = $filename.'.'.$extension;

    file_put_contents('../tmp/' . $file, file_get_contents($list[$i]));

    $picture = new pic_resize();

    $picture->load('../tmp/'.$file);

    $picture->resizeToWidth(640);

    $picture->save('../images/640x320/'.$slug.'/'.$file, $picture->image_type);

    $picture->resizeToWidth(310);

    $picture->save('../images/310x205/'.$slug.'/'.$file, $picture->image_type);

    unlink('../tmp/'.$file);

    array_push($new_list, $file);

    $i++;
}

PHP サイズ変更クラス

    class pic_resize{

   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, $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,9,PNG_FILTER_PAETH);
      }   
      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);
         imagealphablending($this->image, false);
        imagesavealpha($this->image, true);

      }   
   }
   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);
      imagealphablending($new_image, false);
      imagesavealpha($new_image, true);

      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());


      $this->image = $new_image;   
   }      
}
4

1 に答える 1

0

3 番目のパラメーター:
JPEG 品質: 圧縮レベル: 0 (最大の圧縮) から 100 (最小の圧縮)。
http://www.php.net/manual/en/function.imagejpeg.php

PNG 品質: 圧縮レベル: 0 (圧縮なし) から 9。
http://www.php.net/manual/en/function.imagepng.php

jpeg と png では、品質/サイズの設定がかなり異なる/後方にあることに注意してください。

于 2013-04-27T19:24:08.470 に答える