Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP iMagick image compression

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.

This is the code I'm using so far:

function scale_image($size = 200,$extension)
{
    if(!file_exists(ALBUM_PATH . $this->path . $this->filename . $extension))
    {
        $im = new imagick(ALBUM_PATH . $this->path . $this->filename);
        
        $width = $im->getImageWidth();
        $height = $im->getImageHeight();
        if($width > $height)
            $im->resizeImage($size, 0, imagick::FILTER_LANCZOS, 1); 
        else 
            $im->resizeImage(0 , $size, imagick::FILTER_LANCZOS, 1); 
        
        $im->setImageCompression(true);
        $im->setCompression(Imagick::COMPRESSION_JPEG);
        $im->setCompressionQuality(20); 
        
        $im->writeImage(ALBUM_PATH . $this->path . $this->filename . $extension); 
        $im->clear(); 
        $im->destroy(); 
    }
}
like image 784
Rob Forrest Avatar asked Dec 09 '22 18:12

Rob Forrest


1 Answers

Try this:

$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(20);
like image 153
kovshenin Avatar answered Dec 13 '22 23:12

kovshenin