wordpress-develop icon indicating copy to clipboard operation
wordpress-develop copied to clipboard

Add support for HEIC uploading + JPEG conversion

Open adamsilverstein opened this issue 1 year ago • 20 comments

First pass, excluding tests

Trac ticket: https://core.trac.wordpress.org/ticket/53645


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

adamsilverstein avatar Jul 15 '24 23:07 adamsilverstein

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props adamsilverstein, noisysocks, swissspidy, spacedmonkey, peterwilsoncc.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

github-actions[bot] avatar Jul 15 '24 23:07 github-actions[bot]

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance, it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

github-actions[bot] avatar Jul 15 '24 23:07 github-actions[bot]

Thanks for the PR @adamsilverstein. It doesn't seem to work when I test it, though.

If I drop a .heic into the gallery I see an error, the image cannot be previewed, and the file type is still image/heic.

https://github.com/user-attachments/assets/6a11403b-c794-439c-8cb6-59ebf104817f

noisysocks avatar Jul 16 '24 01:07 noisysocks

Thanks for the PR @adamsilverstein. It doesn't seem to work when I test it, though.

@noisysocks some parts were missing, I had a little trouble debugging, but finally got it working. Can you give it another test?

adamsilverstein avatar Jul 16 '24 17:07 adamsilverstein

Here is a screencast of uploading an HEIC image via drag and drop onto the editor:

https://github.com/user-attachments/assets/600766e8-bb07-4fa8-ad88-3dfad79139e1

on the right side you can see all of the files being generated in jpeg format. Note the preview functionality still isn't working correctly.

Also, using the "upload" selector in Gutenberg fails because HEIC is still not a recognized type there:

image

This probably needs fixing in Gutenberg.

adamsilverstein avatar Jul 16 '24 17:07 adamsilverstein

Note the preview functionality still isn't working correctly.

Yep we'll need to fix those two issues in the Gutenberg repo.

Also, using the "upload" selector in Gutenberg fails because HEIC is still not a recognized type there:

This seems to work for me. Might be browser dependent. We can investigate on the Gutenberg side.

noisysocks avatar Jul 17 '24 05:07 noisysocks

I'm seeing a different error about the image size now when I test with a picture I took the other day.

https://github.com/user-attachments/assets/1643092f-4c6c-40b4-b8d9-a8469cbe68ee

Error log:

2024/07/17 05:13:36 [warn] 2197#2197: *2317 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000026, client: 192.168.228.6, server: , request: "POST /wp-admin/async-upload.php HTTP/1.1", host: "wordpress-develop.ddev.site", referrer: "https://wordpress-develop.ddev.site/wp-admin/upload.php"
[17-Jul-2024 05:13:36] WARNING: [pool www] child 10848 said into stderr: "NOTICE: PHP message: PHP Fatal error:  Uncaught Error: Undefined constant "IMAGETYPE_HEIC" in /var/www/html/build/wp-includes/media.php:5607"
[17-Jul-2024 05:13:36] WARNING: [pool www] child 10848 said into stderr: "Stack trace:"
[17-Jul-2024 05:13:36] WARNING: [pool www] child 10848 said into stderr: "#0 /var/www/html/build/wp-admin/includes/image.php(803): wp_getimagesize('/var/www/html/b...')"
[17-Jul-2024 05:13:36] WARNING: [pool www] child 10848 said into stderr: "#1 /var/www/html/build/wp-admin/includes/media.php(392): wp_read_image_metadata('/var/www/html/b...')"
[17-Jul-2024 05:13:36] WARNING: [pool www] child 10848 said into stderr: "#2 /var/www/html/build/wp-admin/includes/ajax-actions.php(2632): media_handle_upload('async-upload', NULL, Array)"
[17-Jul-2024 05:13:36] WARNING: [pool www] child 10848 said into stderr: "#3 /var/www/html/build/wp-admin/async-upload.php(33): wp_ajax_upload_attachment()"
[17-Jul-2024 05:13:36] WARNING: [pool www] child 10848 said into stderr: "#4 {main}"
[17-Jul-2024 05:13:36] WARNING: [pool www] child 10848 said into stderr: "  thrown in /var/www/html/build/wp-includes/media.php on line 5607"
2024/07/17 05:13:36 [error] 2197#2197: *2317 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Undefined constant "IMAGETYPE_HEIC" in /var/www/html/build/wp-includes/media.php:5607
Stack trace:
#0 /var/www/html/build/wp-admin/includes/image.php(803): wp_getimagesize('/var/www/html/b...')
#1 /var/www/html/build/wp-admin/includes/media.php(392): wp_read_image_metadata('/var/www/html/b...')
#2 /var/www/html/build/wp-admin/includes/ajax-actions.php(2632): media_handle_upload('async-upload', NULL, Array)
#3 /var/www/html/build/wp-admin/async-upload.php(33): wp_ajax_upload_attachment()
#4 {main}
  thrown in /var/www/html/build/wp-includes/media.php on line 5607" while reading response header from upstream, client: 192.168.228.6, server: , request: "POST /wp-admin/async-upload.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm.sock:", host: "wordpress-develop.ddev.site", referrer: "https://wordpress-develop.ddev.site/wp-admin/upload.php"

Looks like media.php references IMAGETYPE_HEIF but combat.php defines IMAGETYPE_HEIC.

noisysocks avatar Jul 17 '24 05:07 noisysocks

Wiki says .heic, .heif, image/heic and image/heif are all valid. Should we be liberal in what we accept? In practice I've only ever seen .heic and image/heic.

https://en.wikipedia.org/wiki/High_Efficiency_Image_File_Format

noisysocks avatar Jul 17 '24 05:07 noisysocks

OK, it's working well for me with this patch applied:

diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php
index f7c1a85871..bd4f1b69dc 100644
--- a/src/wp-includes/compat.php
+++ b/src/wp-includes/compat.php
@@ -514,6 +514,6 @@ if ( ! defined( 'IMG_AVIF' ) ) {
 }
 
 // IMAGETYPE_HEIF constant is not yet defined in PHP as of PHP 8.3.
-if ( ! defined( 'IMAGETYPE_HEIF' ) ) {
-	define( 'IMAGETYPE_HEIF', 99 );
+if ( ! defined( 'IMAGETYPE_HEIC' ) ) {
+	define( 'IMAGETYPE_HEIC', 99 );
 }

I tested a bunch of different upload flows. Here's how it looks:

https://github.com/user-attachments/assets/f1c3d19b-fd5d-46e4-a2fc-d35945653a9c

I like that this approach means that you can download the original HEIC image via the attachment page. Very nice.

👍 LGTM once that define() is fixed.

noisysocks avatar Jul 17 '24 05:07 noisysocks

Here's a fix for the Image block: https://github.com/WordPress/gutenberg/pull/63643

noisysocks avatar Jul 17 '24 06:07 noisysocks

Interestingly, in my project I just saw some differences in the browser tests where the browser-determined mime type was once image/heic and once image/heif. Strangely that was for the same image 🤔

I don't know how the mime type detection works in PHP, but it might be worth testing this with a few different HEIC images to see if the image/heif mime type also needs to be checked (in addition to just image/heic).

HEIC images have a different FourCC code depending on the type:

'mif1', // .heic / image/heif
'msf1', // .heic / image/heif-sequence
'heic', // .heic / image/heic
'heix', // .heic / image/heic
'hevc', // .heic / image/heic-sequence
'hevx', // .heic / image/heic-sequence

swissspidy avatar Jul 17 '24 14:07 swissspidy

Looks like media.php references IMAGETYPE_HEIF but combat.php defines IMAGETYPE_HEIC. Ah, right. sticking with HEIC for now, fixed in a65c937c5e6cb92de8ca76ca36d0d20a44c9430e

adamsilverstein avatar Jul 17 '24 15:07 adamsilverstein

Interestingly, in my project I just saw some differences in the browser tests where the browser-determined mime type was once image/heic and once image/heif. Strangely that was for the same image 🤔

I don't know how the mime type detection works in PHP, but it might be worth testing this with a few different HEIC images to see if the image/heif mime type also needs to be checked (in addition to just image/heic).

HEIC images have a different FourCC code depending on the type:

'mif1', // .heic / image/heif
'msf1', // .heic / image/heif-sequence
'heic', // .heic / image/heic
'heix', // .heic / image/heic
'hevc', // .heic / image/heic-sequence
'hevx', // .heic / image/heic-sequence

Currently I'm using this bit of code which I haven't really verified works beyond a single test image. This will be important to test throughly because PHP won't properly detect the mime type.

I plan to add tests next and can bundle several test images to make sure they are identified correctly (and hopefully that we can get the sizes correctly with Imagick). I'll be looking for test images that have a compatible license and are relatively small, contributions welcome!

adamsilverstein avatar Jul 17 '24 15:07 adamsilverstein

Here's a fix for the Image block: WordPress/gutenberg#63643

Amazing, thank you!

adamsilverstein avatar Jul 17 '24 15:07 adamsilverstein

This works well in my testing. I really like that you get to the original image from the attachment screen.

Screenshot 2024-07-17 at 22 38 48

spacedmonkey avatar Jul 17 '24 21:07 spacedmonkey

Worth noting that I tried uploading heif from https://filesampleshub.com/format/image/heif. I was unable to upload and I get this error message. Screenshot 2024-07-17 at 22 48 03

spacedmonkey avatar Jul 17 '24 21:07 spacedmonkey

This works well in my testing. I really like that you get to the original image from the attachment screen.

Screenshot 2024-07-17 at 22 38 48

yum!

adamsilverstein avatar Jul 17 '24 21:07 adamsilverstein

Worth noting that I tried uploading heif from filesampleshub.com/format/image/heif. I was unable to upload and I get this error message. Screenshot 2024-07-17 at 22 48 03

Thanks for the link to the sample files, that is helpful.

I could try to extend full support to .heif files. Do we know how common these are or where/how they are created? I double checked and the files my iphone was creating before I switched it off were .heic files.

adamsilverstein avatar Jul 17 '24 21:07 adamsilverstein

As an experiment I took a .heic image from my iPhone and renamed it to .heif. Regardless of whether the file is .heic or .heif it works the same in Preview and other Apple apps. In both cases the MIME type that file -I spits out is image/heic.

$ file -I playground-1.HEIF
playground-1.HEIF: image/heic; charset=binary
$ file -I playground-2.HEIC
playground-2.HEIC: image/heic; charset=binary

I think it makes sense to have WordPress do the same. Support both .heic and .heif but map them to image/heic in both cases.

noisysocks avatar Jul 18 '24 01:07 noisysocks

Other sample images

https://filesampleshub.com/format/image/heic https://github.com/tigranbs/test-heic-images/tree/master https://www.learningcontainer.com/sample-heic-files-download/# https://github.com/dsoprea/heic-exif-samples

spacedmonkey avatar Jul 18 '24 22:07 spacedmonkey

Added some tests @adamsilverstein.

I think coverage is okay, though I struggled to write one for the changes to wp_generate_attachment_metadata. Ended up deciding it's not worth the effort since it more or less is already covered by test_resize_heic.

Something strange I noticed is that our PHP 8.3 container in GitHub Actions doesn't have the Imagick extension enabled but the other containers do 🤷‍♂️

noisysocks avatar Aug 01 '24 06:08 noisysocks

This still tests really well for me. Here's how things look now that https://github.com/WordPress/gutenberg/pull/63643 is merged.

https://github.com/user-attachments/assets/6cfcda0c-d04b-445f-9884-677f539aac2a

I'll commit on Monday if there's no objections. Keen to get something in early for 6.7 so that there's plenty of time for feedback and iteration.

noisysocks avatar Aug 02 '24 02:08 noisysocks

Committed in r58849.

noisysocks avatar Aug 05 '24 04:08 noisysocks