0

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

  • php-vips has avif write, if that's an option. You'd do eg. `$image->writeToFile("x.avif", ["Q" => 80, "effort" => 9]);` See https://github.com/libvips/php-vips – jcupitt Sep 01 '22 at 09:16
  • Thanks for the tip! I am on a shared hosting, Using phpinfo, I see that Imagick already supports the vips image format, so I suppose the libvips library is available... but I don't know how I could add php-vips? – AbsurdePhoton Sep 01 '22 at 09:34
  • Ah it probably won't work on a shared host, you'd need to be able to add the libvips package. imagick support the vips file format, but not libvips itself. – jcupitt Sep 01 '22 at 09:39

1 Answers1

0

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

There are two functions to set the compression quality :

  • setImageCompressionQuality
  • setCompressionQuality

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