Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create image sub-sizes in additional MIME types using sources for storage. #147

Merged
merged 37 commits into from Feb 24, 2022

Conversation

mitogh
Copy link
Member

@mitogh mitogh commented Feb 3, 2022

Summary

The property is added to every single image size available, each value inside the properties array is a reference to
a different image mime type.

If the image size was created as a JPEG version a WebP version would be created, if a WebP version exists a JPEG version
would be created instead based on the details from ticket #142

This mechanism would allow extending the sources property for more additional mime types.

Fixes:

Relevant technical choices

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

The property is added to every single image size available,
each value inside the properties array is a reference to
a different image mime type.

If the image size was created as a JPEG version a WebP version
would be created, if a WebP version exists a JPEG version
would be created instead based on the details from ticket #142

This mechanism would allow extending the sources properties for
more additional mime types.

The value for each mime type is an associative array with `file` as
the only property for now. And the plan is to include more meaningful
properties such as `filesize` to a different mime type.
@mitogh mitogh added the [Focus] Images Issues related to the Images focus area label Feb 3, 2022
Make sure file follows the standard from `phpcs`
@mitogh mitogh added this to the 1.0.0-beta.1 milestone Feb 3, 2022
@mitogh mitogh added this to Backlog in [Focus] Images via automation Feb 3, 2022
Prevent to use numeric indexes for each image size, instead use the
correct image name.
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitogh This looks like a good start, however there are a couple things that need to be refined. I left some comments.

modules/images/webp-uploads/load.php Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
@felixarntz felixarntz added the [Type] Enhancement A suggestion for improvement of an existing feature label Feb 17, 2022
@felixarntz
Copy link
Member

felixarntz commented Feb 17, 2022

Summarizing a chat conversation on this between @mitogh, @adamsilverstein and myself:

  • We're in agreement to proceed with this approach since it's a more appropriate way to store the additional MIME type images, we can probably close the alternative approach Store the backup JPEG images in _wp_attachment_backup_sizes #154 then.
  • One high-level enhancement that still needs to be added to this PR before it can be merged is ensure that the new files are also being deleted when the image is deleted. We need to hook into the delete_attachment action for that.
  • The other thing that needs to be added is that for every new file generated, we need to update the attachment metadata immediately after, to allow for the process to fail gracefully and to be able to proceed with only the missing sizes on next invokation:
    • Introduce a function to check for which sizes are still missing in the extra MIME type(s) to generate. For this we can probably create a copy of WP core's wp_get_missing_image_subsizes() function and add a MIME type parameter to it. For an eventual core merge, the real function could then be enhanced similarly.
    • Adjust the logic in our wp_generate_attachment_metadata filter callback to always only generate versions for the missing MIME types, using the above function.
    • Call wp_update_attachment_metadata() every time a new image sub-size file is generated, i.e. store the sources field just for that image sub-size file generated and immediately store it.
  • In a separate PR later, we need to integrate this approach also with the feature of editing images, i.e. when an image is edited within WordPress, we also need to generate versions in the additional MIME types for the edited versions, and we need to make sure the original sources are then included in the _wp_attachment_backup_sizes.

mitogh and others added 8 commits February 17, 2022 11:52
Use a function that already exists in core, no need to define a new
function if core already provides this information.
Based on feedback on code review hooks where moved closer where
the callbacks for each is defined.
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
…om:WordPress/performance into feature/142-multiple-mimes-for-image-sizes
Make sure function is only called once, due `wp_get_registered_image_subsizes`
does not return a function.
This class is no longer required or used in the set of tests, so
there's no real value in keeping the class in place.
The added images are going to be used as part of the
tests suite.
The `load` method is required by the editor to process
an image.
The result of the conditional can be used instead.
For every single image size a new mime type would be created
if the original mime type of the image supports the specified
mime type.

Currently this only works with: `image/jpg` and `image/webp`
and vice-versa. Only for all the available image sizes except
the full size image.

This PR fixes: #142, #8
When an attachment is deleted all the generated WebP versions
of an attachment should be removed when the main attachment is removed.
Make sure the file follows the same format as the rest of the project
by following the same guidelines.
@mitogh mitogh marked this pull request as ready for review February 21, 2022 21:42
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitogh I left a few last nit-pick comments. Other than those, the one critical thing here is that I still think the cron usage isn't really appropriate here, since we should tie into WP core behavior, which doesn't have that. See my comment https://github.com/WordPress/performance/pull/147/files#r813258590 for more details.

modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
mitogh and others added 6 commits February 23, 2022 15:10
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Move the WebP image generation into a single process instead
of creating individual cron for each image.
Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work!

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitogh All great now! Except can you please replace all the remaining usages of array_key_exists here with isset as outlined before?

Use `isset` as we are just interested if the key exists even if is `null`
@mitogh
Copy link
Member Author

mitogh commented Feb 24, 2022

Sure @felixarntz just updated the code based on your feedback.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @mitogh! 🎉

Let's proceed in #188 💪

@felixarntz felixarntz merged commit 48845c7 into trunk Feb 24, 2022
[Focus] Images automation moved this from Review to Done Feb 24, 2022
@mitogh mitogh deleted the feature/142-multiple-mimes-for-image-sizes branch February 25, 2022 19:15
@felixarntz felixarntz changed the title Add the sources property to each image size Create image sub-sizes in additional MIME types using sources for storage. Feb 25, 2022
@christianmagill
Copy link

I see a lot of these issues are merged and closed and I'm not sure where the trail ends. Was this ever introduced into a release?

@bethanylang
Copy link
Contributor

Hi @christianmagill! The easiest way to tell if a PR/issue was released is via the Milestone field in the right sidebar – this was included in v1.0.0.

@christianmagill
Copy link

christianmagill commented Jun 8, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Focus] Images Issues related to the Images focus area [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants