Imagick::setImageCompressionQuality

(Информация о версии неизвестна, возможно, только в SVN)

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 при ошибке.

Коментарии

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
2010-03-28 14:35:41
http://php5.kiev.ua/manual/ru/imagick.setimagecompressionquality.html
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();
2012-08-23 06:23:25
http://php5.kiev.ua/manual/ru/imagick.setimagecompressionquality.html
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.
2018-11-08 23:30:56
http://php5.kiev.ua/manual/ru/imagick.setimagecompressionquality.html
Автор:
The setImageCompressionQuality method accepts an integer value as its argument, representing the desired compression quality for the image, from 0 to 100, where 0 represents the lowest quality (highest compression, most lossy), and 100 represents the highest quality (lowest compression, lossless).

* Quality 0: Lowest quality, highest compression, most lossy. This will result in a very small file size, but the image will likely have noticeable artifacts and reduced clarity.

* Quality 100: Highest quality, lowest compression, lossless. The image will retain all its original details, but the file size will be larger.

In most cases, a quality value between 70 and 90 provides a good balance between file size and image quality. However, the optimal value depends on your specific use case and the level of compression you can tolerate while maintaining acceptable image clarity. It's best to experiment with different quality values to find the one that best suits you
2023-07-04 14:35:56
http://php5.kiev.ua/manual/ru/imagick.setimagecompressionquality.html
Автор:
Tested on php 8.1.25, ImageMagick 7.1.1-22
`setImageCompressionQuality()` does just nothing on an `avif` image.
Whatever value you set, the result image is always the same size.
2023-12-22 16:57:50
http://php5.kiev.ua/manual/ru/imagick.setimagecompressionquality.html

    Поддержать сайт на родительском проекте КГБ