title: PHP iMagick image compression
body: <p>I'm fairly new to iMagick and have only found very limited documentation on the PHP library.  I'm happily resizing images and writing them back to the hard drive, but I'm failing completely to compress the images using JPG for instance.</p>
  <p>This is the code I'm using so far:</p>
  <pre><code>function scale_image($size = 200,$extension)
  {
      if(!file_exists(ALBUM_PATH . $this-&gt;path . $this-&gt;filename . $extension))
      {
          $im = new imagick(ALBUM_PATH . $this-&gt;path . $this-&gt;filename);
          
          $width = $im-&gt;getImageWidth();
          $height = $im-&gt;getImageHeight();
          if($width &gt; $height)
              $im-&gt;resizeImage($size, 0, imagick::FILTER_LANCZOS, 1); 
          else 
              $im-&gt;resizeImage(0 , $size, imagick::FILTER_LANCZOS, 1); 
          
          $im-&gt;setImageCompression(true);
          $im-&gt;setCompression(Imagick::COMPRESSION_JPEG);
          $im-&gt;setCompressionQuality(20); 
          
          $im-&gt;writeImage(ALBUM_PATH . $this-&gt;path . $this-&gt;filename . $extension); 
          $im-&gt;clear(); 
          $im-&gt;destroy(); 
      }
  }
  </code></pre>