• Would be very grateful for some advice. We are running latest version of WordPress however keep encountering AVIF file issues. We wish to use AVIF over WebP as the image resolution is better, however WordPress returns them as Jpegs and poorly compressed with poor resolution.

    We have upgraded the server PHP to 8.3, so it should be fully compatible with AVIF since it was meant to work/be fully compatible from the 8.2 PHP version onwards. We would like to get the AVIF issue finally resolved, as we thought it should be straightforward, but it has proven not to be. On the AVIF, we spotted that WordPress makes the AVIF-scaled image as an AVIF, but then the rest are slower and made as JPG.

    Please advise. Many thanks again.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @simonjoy75

    Sorry to hear about the troubles you’re seeing with AVIF. That surely sounds pesky. I did some digging and I’m happy to provide you some guidance, which I hope will fix these issues.

    The issue with WordPress returning AVIF images as poorly compressed, poor-resolution JPEGs, may be due to the following reasons:

    • Your server supports AVIF only partially (unlikely as you said that it runs PHP 8.3 with full AVIF support)
    • WordPress uses JPEG as a fallback option when it cannot handle certain options such as AVIF resizing (most likely the cause of the issue)
    • Lack of full AVIF support in the image library being used to generate AVIF (which is either GD or Imagick).

    I did some research for you and can suggest the following solutions:

    [Easiest Solution] Set up a custom plugin

    I would suggest using the Modern Image Formats plugin from the WordPress Performance Team, which sets image uploads to AVID by default. Once the plugin is installed, you can regenerate your existing JPEG thumbnails to AVIF using something like Regenerate Thumbnails. I think this might be the shorest, and easiest fix for your issue.

    With that said, if you want to get your hands dirty, here are a few additional steps (I recommend doing all three), which might also fix these issues…

    Prevent JPEG Fallback
    This can be done by adding a custom code like so in your functions.php file or a custom code snippet plugin (like this one):

    add_filter('wp_image_editor_output_format', function($formats) {
    $formats['image/avif'] = 'image/avif'; // Force AVIF output
    return $formats;
    });

      This would prevent JPEG fallback altogether.

      Improve Compression Quality

      I see that your issue is with compression quality. You can hard set this with a snippet like so:

      add_filter('jpeg_quality', function($quality) {
      return 100; // Set to maximum quality
      });

      add_filter('wp_editor_set_quality', function($quality, $mime_type) {
      if ($mime_type === 'image/avif') {
      return 90; // Set AVIF quality to 90
      }
      return $quality;
      }, 10, 2);

      Set your Image Editor Library to Imagick

      WordPress typically uses the GD Library or Imagick for image processing. Imagick is known to be much better compared to GD for AVIF. You might want to use a snippet like this to “force” Imagick for better compression:

      add_filter('wp_image_editors', function($editors) {
      return ['WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'];
      });

      P.S. I have not tested this code on a live environment. I would strongly recommend that you try out any code in a local or staging environment before implementing it on a live site.

      I hope that helps! Here’s wishing that all these issues with AVIF get sorted out asap!

      We have tried the above mentioned codes but still the image quality is down . We have upload the AVIF image and generated the image for multiple resolution using thumbnails generate and quality is downgraded . Please provide the solution.

      @bhartir Thanks for sharing more about this issue. Which version of WordPress is the site on? I know of a bug which prevented the snippet from not working, but that should be sorted out now. To fix this issue, I would recommend that:

      • You are on the latest WordPress version (6.7)
      • The image library being used is Imagick and not GD

      You might also want to install the Modern Image Uploads Plugin: https://ww.wp.xz.cn/plugins/webp-uploads/ and then try regenerating thumbnails to see if that fixes the issue.

      If you were on an older version of WordPress, there was a bug that caused this issue: https://core.trac.ww.wp.xz.cn/ticket/61614 – but it looks like this has been resolved?

      Also you can find the snippets I shared with you here: https://make.ww.wp.xz.cn/core/2024/02/23/wordpress-6-5-adds-avif-support/ – I’d recommend trying out some of them. Setting the AVIF quality to 100 instead of 90 might also help, I reckon. I would also double-check for AVIF support in your server in this case, and also if your images are indeed being converted to AVIF or stuck with being JPEG.

    Viewing 3 replies - 1 through 3 (of 3 total)

    The topic ‘WordPress compatibility issues with AVIF file formatss’ is closed to new replies.