phpimagemagickcompressionimagickavif

PHP Imagick AVIF image quality options not working


I have this code to save an AVIF image :

if (class_exists('Imagick')) {
        $image = new Imagick();
        $image->readImage($file); // $file was defined before

        $image->setImageFormat('avif');
        //$image->setImageCompressionQuality($compression_quality); // defined before, default $compression_quality = 25
        //$image->setOption('heic:speed', 5);
        $image->writeImage($output);
    }

I commented out the two options with which I tried to control the AVIF image quality. Whichever option I'm using, or both of them, the produced image size is the same. The image successfully appears under Chrome or Firefox.

I read the code here : https://github.com/ImageMagick/ImageMagick/blob/main/coders/heic.c and it looks to me that both options should work...

I can't use the php 8.1 imageavif function so Imagick is the only option left for me (don't bother answering 'use imageavif')

Versions used on my dev computer : Ubuntu 22.04, Imagemagick 7.1.0, Imagick 3.7.0, libheif 1.12

Could anyone show me how to save an AVIF image and control compression and speed using Imagick ? (and please show me how to use the chroma option if you have the time, 4:2:2 looks fine to me)

Thanks


Solution

  • I found the solution, Thanks to Danack from Imagick https://github.com/Imagick/imagick/.

    There are two functions to set the compression quality :

    According to the online documentation https://www.php.net/manual/en/imagick.setcompressionquality.php (and that doesn't seem logic to me) **setCompressionQuality **is for new images, and setImageCompressionQuality for already existing ones.

    So the code above should work... but it doesn't. It worked as soon as I used setCompressionQuality. Silly! https://www.youtube.com/watch?v=3ANufwUPFm8