Imagick::setImageCompressionQuality

(No version information available, might only be in Git)

Imagick::setImageCompressionQualitySets the image compression quality

Опис

bool Imagick::setImageCompressionQuality ( int $quality )

Sets the image compression quality.

Параметри

quality

The image compression quality as an integer

Значення, що повертаються

Повертає TRUE в разі успіху.

Помилки/Винятки

Кидає ImagickException в разі помилки.

Приклади

Приклад #1 Imagick::setImageCompressionQuality()

<?php
function setImageCompressionQuality($imagePath$quality) {
    
$imagick = new \Imagick(realpath($imagePath));
    
$imagick->setImageCompressionQuality($quality);
    
header("Content-Type: image/jpg");
    echo 
$imagick->getImageBlob();
}

?>

add a note add a note

User Contributed Notes 4 notes

up
4
vhpvhp at msn dot com
5 years ago
For a PNG image, `setImageCompressionQuality()` is not work at all, length of the generated files are totally same.

But I found an effective way, that is use `setOption('png:compression-level', 9)`, the value range is 0-9.
up
4
dylan dot arnold at gmail dot com
11 years ago
I had strange results when trying to get good png compression.

I used Imagick::COMPRESSION_ZIP

Here are a few file sizes for a few compression levels.

0: 236,100
1: 274,035
9: 258,379
50: 241,282
100: 240,156

Note the best result at 0. Also I got thrown off for a long time trying to get similar results to gimp. Make sure your image depth is set to 8, it appears to be 16 bits per channel, at least for me.

You can check with identify -verbose filename.png

You can also strip a bit more file size with $image->stripImage();
up
1
snipes2083 [at] yahoo com
13 years ago
This example shows how to set the compression type, set the compression quality, create a thumbnail and remove unnecessary data in order to reduce file size.

This will use the following functions in reference:
Imagick::setImageCompression
Imagick::setImageCompressionQuality
Imagick::stripImage
Imagick::thumbnailImage
Imagick::writeImage

<?php
    $image
= 'image.jpg';
   
$directory = '/path/to/image';
   
$image_location = $directory . "/" . $image;
   
$thumb_destination = $directory . "/t" . $image;
   
$compression_type = Imagick::COMPRESSION_JPEG;
  
   
$im = new Imagick($image_location);
   
$thumbnail = $im->clone;

   
$thumbnail->setImageCompression($compression_type);
   
$thumbnail->setImageCompressionQuality(40);
   
$thumbnail->stripImage();
   
$thumbnail->thumbnailImage(100,null);
   
$thumbnail->writeImage($thumb_destination);
?>

Now, obviously you don't have to do so much with the variables and the file location.  I only used so many to demonstrate where the images are coming from and where they are going.

NOTE:  The $thumbnail->thumbnailImage(100,null); keeps the aspect ration by setting the second parameter to null.  Read about this at Imagick::thumbnailImage

There is another way to create thumbnails that works quite well if you want to crop the image rather than using the entire image.  Check out Imagick::cropThumbnailImage
up
-41
Anonymous
11 years ago
The default value fot the JPEG compression quality appears to be 86.
To Top