-1

I'm resizing and saving multiple images from a URL and was wondering how I can compress these images more as the images that are saving in the 640x320 folder are 400kb which is too big and was wondering how I can compress these images more, thanks in advance for any advice!

PHP RESIZE AND SIZE HANDLER

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 RESIZE CLASS

    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;   
   }      
}
user2201765
  • 1,017
  • 6
  • 18
  • 21

1 Answers1

0

3rd parameter:
JPEG Quality : Compression level: from 0 (most compression) to 100 (least compression).
http://www.php.net/manual/en/function.imagejpeg.php

PNG Quality : Compression level: from 0 (no compression) to 9.
http://www.php.net/manual/en/function.imagepng.php

Note that that the quality/size settings are pretty much different/backwards between jpeg and png

Brad Kent
  • 4,982
  • 3
  • 22
  • 26
  • I have it set at 9, but its filesize is 411kb and the original is 620kb is there a way I can compress this more as 200kb isnt really anything – user2201765 Apr 27 '13 at 19:36