Skip to content

When uploading, generate both WebP and jpeg format images by default #142

@adamsilverstein

Description

@adamsilverstein

Feature Description

Based on the decision in #96 (comment) we need to work on enabling generating both formats by default.

Specifically we need to address:

1. When the user uploads a JPEG image, we generate the smaller image sizes in both JPEG and WebP by default.
2. When inserting an image into a post or including an image in the frontend through any other means, we use the WebP version whenever it is available.
3. Developers can opt out of generating JPEG or WebP, e.g. if they are certain they won't need one of these versions.

  1. Generate both formats
  • We can hook on wp_generate_attachment_metadata to generate the additional mime type images (some example code from @erikyo).
  • If the user uploads a jpeg and generates jpeg sub sizes, we would hook in and also generate WebP sizes. If the user uploads a WebP core will generate WebP sub sizes, we would hook in and generate jpeg sub sizes
  • The behavior should be filterable, I propose a new filter named image_editor_output_formats which would return an array of mime types. By default this would return ['image/webp', 'image/jpeg'].
    • The first/default mime type would be used for the default sub-sizes generated by default and stored in the sizes array (If only one mime type is given, only that type is generated).
    • When more than one mime type is returned, the second and subsequent types are used to generate additional sub sizes and their data stored in the additional_sizes array...

1b. In addition to generating the new images, we also need to store meta data for new mime type sizes

  • for backwards compatibility reasons, I suggest we store a single mime type in the sizes array
  • additional mime type sizes we generate should be stored separately, in a key I propose naming additional_sizes
  • mime type data will go into a sources array inside each size
  • additional handling will be required for these additional sizes:
    • when the original image is deleted, these sub sizes should also be deleted
    • when missing sub sizes are regenerated, this should include all mime types
    • when the image is cropped/rotated/flipped in the media library, the action should apply to these images as well
    • eventually, functions should be extended or added that leverage these new sizes to generate a picture element
  1. Use WebP on front end
  • This can be achieved by having the default sizes sub sizes be WebP by default, which will be the case with ['image/webp', 'image/jpeg'] as the default value for image_editor_output_formats
  • We should also consider adding a mime type argument or a filter for functions like get_image_tag or wp_get_attachment_image_src as a more direct way for developers to specify the mime type they want to use.
  1. Developers can opt out
  • Adding add_filter( 'image_editor_output_formats', '__return_false' ); would ensure only the uploaded mime type would be use for sub sizes (current WordPress behavior).
  • Adding add_filter( 'image_editor_output_formats', function() { return array( 'image/jpeg', 'image/webp' ); } ); would ensure jpeg and WebP were generated, with jpeg stored in sizes and used as the default output format
  • Adding add_filter( 'image_editor_output_formats', function() { return array( 'image/webp' ); } ); would ensure only WebP sub sizes were generated (current behavior of the webp-uploads module).
  • The filter will reference the original image for context, so output formats could be dynamic

Feedback welcome as well as contributions, please let us know what you plan to work on by leaving a comment.

Metadata

Metadata

Labels

[Plugin] Modern Image FormatsIssues for the Modern Image Formats plugin (formerly WebP Uploads)[Type] EnhancementA suggestion for improvement of an existing feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions