Make WordPress Core

Opened 6 months ago

Closed 5 months ago

Last modified 5 months ago

#61916 closed defect (bug) (invalid)

Improving Image Upload Handling: Automatic Resizing for Large Images

Reported by: maheshmthorat's profile maheshmthorat Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Media Keywords: has-testing-info
Focuses: Cc:

Description

When uploading large images (e.g., those with dimensions exceeding 7500 pixels), the system doesn't automatically crop them to the custom size of 2400 pixels using the following code:

<?php
add_image_size('custom_size_2400', 2400);

Additionally, there are no restrictions on uploading images of any resolution. Currently, to achieve the desired size, you must manually crop the image to a smaller size before it will resize correctly.

To address this, there should be a setting that automatically converts the uploaded image to the maximum allowed size, after which it will resize according to the specified dimensions.

Change History (11)

#1 @hellofromTonya
5 months ago

  • Component changed from General to Media
  • Version 6.6.1 deleted

Hello @maheshmthorat,

Welcome to WordPress Core's Trac.

I'm doing 6.6.x triage today. Resetting the Version as 6.6.1 did not ship changes that might be affecting the reported issue. Once the root cause is identified, then the version that introduced it can be set.

#2 @maheshmthorat
5 months ago

Hi @hellofromtonya,

Thank you for looking into this issue during the 6.6.x triage. I appreciate your help.

To clarify the problem, when uploading large images (e.g., with dimensions over 7500 pixels), they are not being automatically resized to the custom size I’ve specified using add_image_size('custom_size_2400', 2400);. It appears that WordPress isn't automatically cropping or resizing these large images down to the maximum allowed size.

Ideally, I would like the system to automatically resize any uploaded image to a maximum of 2400 pixels on the longest side. This way, we can avoid the need for manual cropping or resizing of images before uploading. Could this be related to changes in how WordPress handles image uploads, or is there another setting I should be aware of to enforce this automatic resizing?

Please let me know if there's any more information you need from me, or if there's a workaround or setting I can adjust in the meantime.

Thank you!

Last edited 5 months ago by maheshmthorat (previous) (diff)

#3 @hellofromTonya
5 months ago

Reproduction Report

This report validates that the issue can be reproduced.

Environment

  • OS: macOS
  • Web Server: Nginx
  • PHP: 8.3.8
  • WordPress: 6.6.2
  • Browser: Firefox 15.4
  • Theme: Twenty Twenty-Four
  • Active Plugins: None

Set up

Image:
I downloaded and used this image which has a size of 8294 × 12440.

For adding the custom size, I added the following file: wp-content/mu-plugins/test.php with this code:

<?php

add_action( 'after_setup_theme', function() {
	add_image_size( 'custom_size_2400', 2400 );
} );

Test Instructions

  1. Log into admin.
  2. Go to Media > Add New Media File.
  3. Select (or drag and drop) the large image to be uploaded.
  4. Once uploaded, check the generated scaled images in the wp-content/uploads/ directory.

🐞 Reported bug occurs here in that there should not be any scaled images with a custom dimension of 2400.

Actual Results

❌ Not able to reproduce the bug.

This image was generated photo-1644433329707-2b528437a7e7-2400x3600.jpg.

The 8294 × 12440 image was scaled to 2400 x 3600.

#4 @hellofromTonya
5 months ago

Reproduction Report

Repeating comment:3 test report but this time specifying both the width and height.

This report validates that the issue can be reproduced.

Environment

  • OS: macOS
  • Web Server: Nginx
  • PHP: 8.3.8
  • WordPress: 6.6.2
  • Browser: Firefox 15.4
  • Theme: Twenty Twenty-Four
  • Active Plugins: None

Set up

Image: Same as comment:3

Code: Same as comment:3, except also specified the height.

<?php

add_action( 'after_setup_theme', function() {
	add_image_size( 'custom_size_2400', 2400, 2400 );
} );

Test Instructions

  1. Log into admin.
  2. Go to Media > Add New Media File.
  3. Select (or drag and drop) the large image to be uploaded.
  4. Once uploaded, check the generated scaled images in the wp-content/uploads/ directory.

🐞 Reported bug occurs here in that there should not be any scaled images with a custom dimension of 2400.

Actual Results

❌ Not able to reproduce the bug.

This image was generated photo-1644433329707-2b528437a7e7-1600x2400.jpg.

The 8294 × 12440 image was scaled to 1600 x 2400.

#5 @hellofromTonya
5 months ago

Reproduction Report

Repeating comment:3 and comment:4 tests report but this time specifying both the width and height and setting crop to true.

This report validates that the issue can be reproduced.

Environment

  • OS: macOS
  • Web Server: Nginx
  • PHP: 8.3.8
  • WordPress: 6.6.2
  • Browser: Firefox 15.4
  • Theme: Twenty Twenty-Four
  • Active Plugins: None

Set up

Image: Same as comment:3

Code: Same as comment:4, except also specifying to crop the image.

<?php

add_action( 'after_setup_theme', function() {
	add_image_size( 'custom_size_2400', 2400, 2400, true );
} );

Test Instructions

  1. Log into admin.
  2. Go to Media > Add New Media File.
  3. Select (or drag and drop) the large image to be uploaded.
  4. Once uploaded, check the generated scaled images in the wp-content/uploads/ directory.

🐞 Reported bug occurs here in that there should not be any scaled images with a custom dimension of 2400.

Actual Results

❌ Not able to reproduce the bug.

This image was generated photo-1644433329707-2b528437a7e7-2400x2400.jpg.

The 8294 × 12440 image was scaled and cropped to 2400 x 2400.

#6 @hellofromTonya
5 months ago

  • Keywords has-testing-info reporter-feedback added

@maheshmthorat, I ran 3 different tests by specifying in add_image_size():

Reference: https://developer.wordpress.org/reference/functions/add_image_size/

Each of the tests worked as expected.

The last test generated a scaled and cropped the image of 2400 x 2400 ✅

So to scale and crop to 2400 x 2400, do this:

add_image_size( 'custom_size_2400', 2400, 2400, true );

Can you check please if changing add_image_size() works for you? Also, do my tests align to how you are testing the issue you're experiencing?

#7 @maheshmthorat
5 months ago

Hi @hellofromTonya,

Thank you for following up on this matter. We have reviewed it, but we are still experiencing issues.

This is our system details https://drive.google.com/file/d/1ePcjuiuUqxhtjVyUu7mmu0EE1bKB0oGo/view?usp=drive_link

When we have uploaded your image we only get single version of image. Please find screen shot here

https://drive.google.com/drive-viewer/AKGpihaiYvf2kBfGP3tp6t4PgP_dFshdQ-afSvMoJD5S1NAUNvM-JjHed9Krl1z1L5ttVQLtOlnS8HGQgUpLO1KlKcU9SWjOA73jd7g=w1866-h922-rw-v1

While uploading the image below, we discovered that multiple versions were created.

https://drive.google.com/drive-viewer/AKGpihZ31SOuhwj-AoH4q1frK_RZ3ezhVkuZmQP08Z4oN7_IsTtntGqKWqmhaBleaL2Dr5l067xhptLEz9L1oFe0XVsfZ548moRmOQ=w1866-h922-rw-v1

With below code

<?php
add_action( 'after_setup_theme', function() {
	add_image_size( 'custom_size_2400', 2400 );
} );

The only difference between these images is their pixel values and image size.

Please let us know if you notice anything we might have overlooked.

Thanks,
Mahesh Thorat

Last edited 5 months ago by maheshmthorat (previous) (diff)

#8 @hellofromTonya
5 months ago

@maheshmthorat as you're seeking to scale and crop the images, please change your code to do this:

<?php
add_action( 'after_setup_theme', function() {
	add_image_size( 'custom_size_2400', 2400, 2400, true );
} );

The code specifies the width (2400), height (2400), and sets it to crop (true).

Can you retest please?

#9 follow-up: @maheshmthorat
5 months ago

Hi @hellofromTonya,

Thank you for your code. This time, the image does get created with variations.

However, I tried using this image (https://drive.google.com/file/d/1zEQ8Z-07EXfjvcrDKJCQBsAV8pPg0ZrD/view?usp=drive_link) and it couldn't create any variations. Could you please download this image and check if it works on your side as well?

I've tried it on different servers without success.

Thanks,
Mahesh Thorat

#10 in reply to: ↑ 9 @hellofromTonya
5 months ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Replying to maheshmthorat:

However, I tried using this image (https://drive.google.com/file/d/1zEQ8Z-07EXfjvcrDKJCQBsAV8pPg0ZrD/view?usp=drive_link) and it couldn't create any variations. Could you please download this image and check if it works on your side as well?

I've tried it on different servers without success.

I tested that image in a local test environment, same as I did the other tests in this ticket. It worked for me.

Using this test code:

<?php
add_action( 'after_setup_theme', function() {
	add_image_size( 'custom_size_2400', 2400, 2400, true );
} );

Here's a list from the uploads directory:

pexels-cottonbro-5658531.jpg
pexels-cottonbro-5658531-150x150.jpg
pexels-cottonbro-5658531-200x300.jpg
pexels-cottonbro-5658531-683x1024.jpg
pexels-cottonbro-5658531-768x1152.jpg
pexels-cottonbro-5658531-1024x1536.jpg
pexels-cottonbro-5658531-1365x2048.jpg
pexels-cottonbro-5658531-2400x2400.jpg
pexels-cottonbro-5658531-scaled.jpg
pexels-cottonbro-5658531.jpg

Notice, it scaled and cropped to 2400 x 2400, pexels-cottonbro-5658531-2400x2400.jpg.

As there are multiple "can't reproduce" reports, I suspect the issue you're experiencing is due to something outside of WordPress Core's source code.

Please note, this Trac is used for the development of the WordPress software, not for assistance with individual sites or plugins. You can find people who are able to assist with your site in the WordPress support forums.

I've closed this ticket as invalid, which is Trac's unfriendly term to indicate I've referred you to the support team.

Note: See TracTickets for help on using tickets.