Opened 23 hours ago
#66261 new defect (bug)
AVIF uploads get JPEG sub-sizes when Imagick (ImageMagick 6) reports the file as HEIC
| Reported by: | st3phan5 | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | Awaiting Review |
| Component: | General | Version: | |
| Severity: | minor | Keywords: | |
| Cc: | Focuses: |
Description
Hi everyone,
I searched the existing tickets before posting and didn't find anything that matches this exactly. Related are #53645 (HEIC to JPEG conversion in 6.7) and #62365 (HEIC mapping vs. image_editor_output_format overrides), but as far as I can tell they cover real HEIC uploads, not AVIF files. If this is a duplicate, please point me to the right ticket.
Summary
On a server with ImageMagick 6.9.11, uploaded AVIF files get their sub-sizes (thumbnail, medium) generated as JPEG instead of AVIF. The reason seems to be that Imagick identifies the AVIF file as HEIC, so the HEIC to JPEG mapping kicks in.
Environment
- WordPress 7.1.3, fresh install, no content
- PHP 8.5.9
- Imagick 3.8.1 with ImageMagick 6.9.11-60 Q16
- GD (bundled) with AVIF support
- Theme: Twenty Twenty-Five
Is this the latest version?
Yes, I tested on a fresh install of 7.1.3.
Does it happen with all plugins deactivated and a default theme?
Yes. The install is new, Twenty Twenty-Five is active and no plugins are active. Debug logging is enabled and nothing is written to the log. image_editor_output_format and image_editor_default_mime_type have no filters attached.
Steps to reproduce
- Use a server where
WP_Image_Editor_Imagickis selected (ImageMagick 6.9.11, AVIF listed in the supported formats). - Upload an AVIF file to the Media Library.
- Look at the attachment metadata (
wp_get_attachment_metadata()) or the uploads folder.
Expected result
The original stays image/avif and the sub-sizes are generated as AVIF (e.g. image-150x150.avif).
Actual result
The original is stored as image/avif, but the sub-sizes are created as JPEG (image-150x150.jpg, "mime-type": "image/jpeg" in the metadata).
What I found when debugging
wp_get_image_mime( $file )returnsimage/avif, and so doesget_post_mime_type().new Imagick( $file )reportsgetImageFormat()=HEICandgetImageMimeType()=image/x-heicfor this AVIF file.- After
wp_get_image_editor( $file ), the editor'smime_typeproperty isimage/heic. - Calling
$editor->save( $path, 'image/avif' )explicitly works fine and writes a valid AVIF. So Imagick can encode AVIF, the problem only occurs in the normal upload flow where no target type is passed. - Site Health lists the default transforms
image/heic → image/jpeg(plus heif and the sequence types).
My guess is that ImageMagick 6 handles AVIF through the libheif delegate and therefore labels it as HEIC. WordPress then seems to take the type from Imagick instead of from the file itself, and the default HEIC to JPEG mapping is applied to what is really an AVIF file. I haven't looked deeper into the code than this, so please correct me if I'm wrong.
Workaround
Forcing GD fixes it, since GD reports the format correctly:
add_filter( 'wp_image_editors', fn() => [ 'WP_Image_Editor_GD' ] );
With that filter, new uploads get AVIF sub-sizes after regenerating thumbnails.
Possible direction
Maybe the editor could use the mime type detected from the file (wp_get_image_mime()) when Imagick returns a HEIC type for a file that WordPress itself identifies as AVIF. ImageMagick 6.9.11 is old, so this may be an edge case, but it is still shipped by some distributions and hosts.
Thanks for taking a look. I can provide a sample AVIF file and more test output if that helps.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)