WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 2 months ago

#35725 new enhancement

Add mime-type for Webp

Reported by: markoheijnen Owned by:
Milestone: 5.3 Priority: normal
Severity: normal Version: 3.5
Component: Media Keywords: has-patch 2nd-opinion needs-testing has-unit-tests needs-refresh
Focuses: Cc:

Description

Would be great that at least WP_Image_Editor (if supported) could be used to transform images to the .webp format. Tickets #24718 and #27789 focus on upload which is something which could be done later.

Attachments (4)

35725.patch (1.5 KB) - added by markoheijnen 4 years ago.
35725.diff (28.1 KB) - added by blobfolio 20 months ago.
Patch with upload_mimes and image editor integration.
phpunit.zip (671.5 KB) - added by blobfolio 20 months ago.
Files for unit testing.
35725.2.diff (28.1 KB) - added by blobfolio 19 months ago.
Just a refresh :)

Download all attachments as: .zip

Change History (19)

@markoheijnen
4 years ago

#1 @markoheijnen
4 years ago

  • Keywords has-patch added

This ticket was mentioned in Slack in #core-images by markoheijnen. View the logs.


4 years ago

This ticket was mentioned in Slack in #core-images by markoheijnen. View the logs.


3 years ago

This ticket was mentioned in Slack in #core-media by mike. View the logs.


20 months ago

#6 @blobfolio
20 months ago

PHP support for WebP is starting to catch up. I'd like to investigate whether or not it is feasible to expand this enhancement to include thumbnail generation support as well.

I'll dive deeper in the next week or so and update the patch accordingly, but in the meantime, here are a few references:

GD and EXIF support:

The output from gd_info() should include ['WebP Support']=>true under environments that support WebP.

Imagick can be compiled with WebP support (e.g. with libwebp-dev, etc.). This does not seem to be very common yet, but we could conditionally check the response of Imagick::queryFormats('WEBP'); a non-empty array() being Yay!, an empty one being Boo!

@blobfolio
20 months ago

Patch with upload_mimes and image editor integration.

@blobfolio
20 months ago

Files for unit testing.

#7 @blobfolio
20 months ago

  • Keywords 2nd-opinion needs-testing has-unit-tests added

The 35725.diff patch adds full WebP support to WordPress:

  • Whitelists webp files for upload;
  • Adds webp to the list of image formats for admin display, thumbnail generation, etc.;
  • Provides numerous workarounds for environments where WebP support exists in a partial state;
  • Supports VP8, VP8L, and VP8X container formats;
  • GD handling (PHP5.6+ for VP8, PHP7.0+ for the rest);
  • ImageMagick handling (works as long as the system's binary was compiled with WebP support);


This patch introduces one new function:

/**
 * Wrapper for getimagesize()
 *
 * The native PHP function might not support newer formats.
 *
 * @since 5.0
 *
 * @see {https://core.trac.wordpress.org/ticket/35725}
 *
 * @param string $file Full path to the file.
 * @param array $info Additional metadata to pull.
 * @return array|false An array containing width, height, type constant, attributes, and media type.
 */
function wp_get_image_size( $file, &$info = array() ) { ... }

All calls to the native getimagesize() function have been redirected to this wrapper to ensure that WebP dimensions are properly reported. This also fixes the inconsistent use of error suppression that existed previously.

This wrapper can later be expanded to account for other new formats, such as #42775.


A few other things to note:

  • Hosts that do not support WebP through GD or ImageMagick will still allow files to be uploaded, they just won't get intermediate sizes.
  • There was once a bug in GD that confused ARGB with RGBA, causing colors to shift. This has been fixed upstream, but some unsupported distributions (like Debian Squeeze) might still be using it.
  • ImageMagick on Arch Linux is currently causing a segfault when dumping a WebP via the getImageBlob() method. As a workaround, stream previews (when e.g. editing an image) output png instead. This doesn't affect the saved results, so seems safe to keep.


One final note:

WebP browser support (https://caniuse.com/#search=webp) is not universal.

This patch restricts its focus to backend handling, but there is opportunity for a future frontend-focused patch to provide jpg/png fallbacks. The use of the HTML5 <picture> element seems like the best approach for such work. As it happens, @desrosj is currently working on general <picture> support for WP, so if/when that lands, a frontend WebP enhancement should be easy. :)

This ticket was mentioned in Slack in #core-media by blobfolio. View the logs.


20 months ago

This ticket was mentioned in Slack in #core-media by blobfolio. View the logs.


20 months ago

@blobfolio
19 months ago

Just a refresh :)

This ticket was mentioned in Slack in #core-media by blobfolio. View the logs.


19 months ago

This ticket was mentioned in Slack in #core by ipstenu. View the logs.


19 months ago

#12 @micasuh
7 months ago

With Firefox 65's WebP support and Edge adopting Chromium and Blink, the end of 2019 could be close to 80% WebP support unless Webkit surprises us. Browsers that don't support WebP will be outliers.

@blobfolio's patches (which I assume can easily be updated again) and @desrosj's work on <picture> would give WP devs an excellent avenue to use WebP in production today and help reduce load times in non-trivial ways.

I think 2019 is the right time for this to move to core.

Last edited 7 months ago by micasuh (previous) (diff)

#13 @SergeyBiryukov
5 months ago

  • Milestone changed from Awaiting Review to 5.3

#14 @desrosj
3 months ago

  • Keywords needs-refresh added

Looks like 35725.2.diff is no longer applying cleanly to trunk.

This ticket was mentioned in Slack in #core-media by melchoyce. View the logs.


2 months ago

Note: See TracTickets for help on using tickets.