Make WordPress Core

Opened 2 years ago

Last modified 2 months ago

#61925 new defect (bug)

subsizes with same dimensions are generated multiple times unnecessarily

Reported by: kkmuffme Owned by:
Priority: normal Milestone: Awaiting Review
Component: Media Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description

https://github.com/WordPress/wordpress-develop/blob/6.6/src/wp-admin/includes/image.php#L461

_wp_make_subsizes will generate the same file with same dimensions multiple times if multiple sizes with the same dimensions/crop are registered.
This unnecessarily slows down the uploads and often is a cause for timeouts too, if there are many sizes registered.

e.g. with WooCommerce where "woocommerce_gallery_thumbnail" might be equal to wp "thumbnail" size.

I checked trac, but couldn't find a related issue, in case there's one please close this one.

Change History (11)

#1 @rohitmathur7
2 years ago

Hii @kkmuffme ,
I tried to replicate the issue but was not able to.

These are the steps I followed:

  • Register multiple new image sizes with same dimensions but different name.
function replicate_subsizes_issue() {
	// Register multiple image sizes with the same dimensions and crop settings
	add_image_size( 'custom-size-1', 800, 600, true );
	add_image_size( 'custom-size-2', 800, 600, true );
	add_image_size( 'custom-size-3', 800, 600, true );
}
add_action( 'after_setup_theme', 'replicate_subsizes_issue' );
  • Added a new image in media library.
  • But for the new image I can only see one image in uploads with dimension 800X600.

Can you please specify the steps to replicate this?
Thanks.

#2 @kkmuffme
2 years ago

But for the new image I can only see one image in uploads with dimension 800X600.

Sorry, but obviously. How would you be able to see multiple files with the same name?
It generates the same 800x600 file multiple times.

Your replication is correct except that you incorrectly check the result. Set a breakpoint at the referenced line and you'll see that it recreates the image multiple times for 800x600

#3 @kkmuffme
22 months ago

#62388 was marked as a duplicate.

#4 @kkmuffme
22 months ago

As reported in https://core.trac.wordpress.org/ticket/62388 for "crop" it doesn't actually crop (see https://core.trac.wordpress.org/ticket/62389#ticket), so the cropped images that have identical dimensions as ones already generated can be skipped too.

This ticket was mentioned in PR #7802 on WordPress/wordpress-develop by @kkmuffme.


22 months ago
#5

  • Keywords has-patch added

#6 @nickchomey
3 months ago

  • Keywords needs-patch added; has-patch removed

I've confirmed this. The sizes need to be de-duped.

I will incorporate it into a PR I'm currently working on related to subsizes

#7 @nickchomey
3 months ago

  • Focuses performance added

#8 @thisismyurl
2 months ago

PR up for this: https://github.com/WordPress/wordpress-develop/pull/12506

Sub-sizes are written to disk named by their dimensions, so two registered sizes with the same width, height, and crop resolve to the same file. _wp_make_subsizes() was calling make_subsize() once per size name, re-encoding that identical file. The patch groups sizes by their resulting file and generates each once, sharing it with every size name that requested it. The stored metadata is unchanged; only the redundant image work is removed. Grouping is by the requested width/height/crop, so only guaranteed-identical files are merged.

Regression test registers two identical sizes plus one distinct size and asserts a single make_subsize() call for the pair, sharing one file.

@ankitmaru commented on PR #12506:


2 months ago
#9

Picked through the diff. Looks like this only touches formatting.php, but wpautop also has a JS version in @wordpress/autop that usually needs the same fix to stay in sync.

The other thing on wpautop is regressions. Do the existing Tests_Formatting_wpAutop cases still pass, or just the new ones? Happy to run it if you haven't.

@ankitmaru commented on PR #12506:


2 months ago
#10

Picked through the diff. Looks like this only touches formatting.php, but wpautop also has a JS version in @wordpress/autop that usually needs the same fix to stay in sync.

The other thing on wpautop is regressions. Do the existing Tests_Formatting_wpAutop cases still pass, or just the new ones? Happy to run it if you haven't.

#11 @thisismyurl
2 months ago

  • Keywords has-patch has-unit-tests added; needs-patch removed
Note: See TracTickets for help on using tickets.