Opened 4 weeks ago
Last modified 13 days ago
#66284 new defect (bug)
image scaling is skipped for large images, resulting in huge thumbnails
| Reported by: | somian | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Media | Version: | 7.1.3 |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
When uploading a large Image to the media gallery, WordPress will simply skip the scaling. No thumbnails etc. are generated and WordPress defaults to the full image instead. This means that It'll use e.g. a 70MB JPG as thumbnails.
How to reproduce:
- upload large image. (I tried with a full size JPG from a Hasselblad X2D II 100C, 70MB JPG that's 102 megapixels).
- observe size in media gallery
expected: image is scaled (default: 2560px)
observed: image is full size!
Attachments (5)
Change History (13)
#1
@
4 weeks ago
I provided a test image here: It's also HDR (which should be supported since WordPress 7.1)
https://somian.net/stuff/bugs/wordpress/large-photo/B0000165.jpg
#2
@
4 weeks ago
Hello @somian,
I tested the reported media upload issue in WordPress Playground, and everything appears to be working as expected, as shown in my attached screenshot. WordPress automatically scales the uploaded image and generates the required thumbnail sizes.
Please provide more details about your environment so that we can investigate further.
My testing environment:
- Clean https://playground.wordpress.net/
- Browser: Brave v1.97.56
- OS: Ubuntu 22.04.5 LTS
- Image: Your provided test imagehttps://somian.net/stuff/bugs/wordpress/large-photo/B0000165.jpg
#3
@
4 weeks ago
Hello @somian,
I tested the provided B0000165.jpg (102MP, ~70MB) in a clean local WordPress environment. Image scaling completed successfully to 2560 × 1920 px, and all standard intermediate image sizes were generated as expected.
I've attached screenshots showing the results. This may be environment-specific, potentially related to PHP memory limits or image editor restrictions.
#4
@
4 weeks ago
Wild. I manage 2 WordPress installations (7.1.3) and I also tested it in the other one and it's the same issue there (Image scaling is skipped only with large JPGs).
It does scale in the Sandbox, however (that is on 7.1.2, though). So it's not my browser here.
If it's a (server) environment thing, can we narrow down what it is? The two WordPress installations where it doesn't scale are hosted by one.com. Perhaps something times out that doesn't time out in the Sandbox?
#5
@
4 weeks ago
I can now reproduce the issue. I tested it on a clean latest WordPress version but this time with the Imagick library enabled. The provided test image is not scaled, and WordPress does not generate the required thumbnail sizes.
However, the process works as expected with smaller images, and the required thumbnail sizes are generated successfully.
I have attached screenshots for reference.
Edit: php v8.4
This ticket was mentioned in PR #14231 on WordPress/wordpress-develop by @therssoftware.
4 weeks ago
#6
- Keywords has-patch has-unit-tests added
### Description
In `WP_Image_Editor_Imagick::thumbnail_image()`, large images are pre-resampled using `sampleImage()` to 5x the destination size before applying high-quality resizing whenever `$resize_ratio < .111` (i.e. output area is less than 1/3^2 of the original image).
However, when downscaling by a factor between 3x and 5x (such as downscaling a 102-megapixel `11656 x 8742` image to the default big image threshold of `2560 x 1920`):
- `$resize_ratio = ~0.048 < .111` is true.
- `$dst_w * 5 = 12,800px` (exceeds the original width of `11,656px`).
- `$dst_h * 5 = 9,600px` (exceeds the original height of `8,742px`).
As a result, `sampleImage()` attempts to **upsample** the image into a 123-megapixel canvas prior to downscaling. This exhausts ImageMagick memory/cache resource limits (`ImagickException: cache resources exhausted`), returning a `WP_Error('image_resize_error')`. In `wp_create_image_subsizes()`, this causes the scaled image generation to silently abort, leaving the multi-megabyte image full-sized with no thumbnails created.
### Solution
1. In `WP_Image_Editor_Imagick::thumbnail_image()`, ensure `sampleImage()` is only called when both intermediate sampled dimensions are strictly smaller than the current image dimensions:
```php
if ( $resize_ratio < .111
&& ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 )
&& ( $dst_w * $sample_factor < $this->size['width'] && $dst_h * $sample_factor < $this->size['height'] )
) {
$this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor );
}
- In
WP_Image_Editor_Imagick::crop(), update$this->sizewith the cropped dimensions ($src_w,$src_h) prior to callingthumbnail_image()so that intermediate resampling operations reflect post-crop dimensions. - Added dedicated unit test suite in
tests/phpunit/tests/image/imagickSampleImage.phpcovering all resampling branches and edge cases (including large image threshold scaling, thumbnail creation, and crop downscaling).
Testing Instructions
- Upload an ultra-high resolution image (e.g. 102MP, 11656x8742) with
Imagickenabled. - Verify that the image is scaled to 2560px (
-scaled.jpg) and all intermediate thumbnails are successfully generated without cache resource exhaustion errors. - Run the unit test suite:
vendor/bin/phpunit tests/phpunit/tests/image/imagickSampleImage.php
---
## WordPress Trac Comment
```markdown
Replying to [comment:5 vishalmori]:
> I can now reproduce the issue. I tested it on a clean latest WordPress version but this time with the Imagick library enabled. The provided test image is not scaled, and WordPress does not generate the required thumbnail sizes.
Thanks for confirming reproduction with Imagick enabled! I investigated the root cause in `WP_Image_Editor_Imagick` and found the issue in `thumbnail_image()`:
{ {{#!php
if ( is_callable( array( $this->image, 'sampleImage' ) ) ) {
$resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] );
$sample_factor = 5;
if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) {
$this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor );
}
}
} }}
=== The Cause
The pre-resample step was designed to speed up large reductions by nearest-neighbor sampling to 5x the destination size before running the quality filter. It triggers whenever `$resize_ratio < .111` (area less than 1/3^2).
However, whenever the linear downscaling factor is between 3x and 5x:
- `$resize_ratio < .111` is true.
- But `5 * $dst_w` is **larger than the original width**!
- And `5 * $dst_h` is **larger than the original height**!
For the reporter's 102MP image (`11656 x 8742`):
- When scaling to `2560 x 1920`, `$dst_w * 5 = 12,800px` and `$dst_h * 5 = 9,600px`.
- `sampleImage()` attempts to **upsample** the 102MP image into a 123MP intermediate canvas.
- This exhausts ImageMagick's cache/memory resources (`ImagickException: cache resources exhausted`), returning `WP_Error('image_resize_error')`.
- In `wp_create_image_subsizes()`, this error causes the big image scaling step to fail, leaving the full-sized image with zero thumbnails generated.
=== The Fix
1. Guard `sampleImage()` so it only runs if both resampled dimensions are strictly smaller than the original:
{ {{#!php
if ( $resize_ratio < .111
&& ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 )
&& ( $dst_w * $sample_factor < $this->size['width'] && $dst_h * $sample_factor < $this->size['height'] )
) {
$this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor );
}
} }}
2. Update `$this->size` immediately after `cropImage()` in `crop()` so subsequent operations use the cropped dimensions.
3. Added dedicated unit test coverage in `tests/phpunit/tests/image/imagickSampleImage.php` verifying that `sampleImage` is correctly skipped when sampled dimensions exceed the source image while continuing to accelerate true large downscales.
#7
follow-up:
↓ 8
@
4 weeks ago
- Keywords has-patch has-unit-tests removed
Replying to vishalmori:
I can now reproduce the issue. I tested it on a clean latest WordPress version but this time with the Imagick library enabled. The provided test image is not scaled, and WordPress does not generate the required thumbnail sizes.
Thanks for confirming reproduction with Imagick enabled! I investigated the root cause in WP_Image_Editor_Imagick and found the issue in thumbnail_image():
if ( is_callable( array( $this->image, 'sampleImage' ) ) ) { $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); $sample_factor = 5; if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) { $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); } }
The Cause
The pre-resample step was designed to speed up large reductions by nearest-neighbor sampling to 5x the destination size before running the quality filter. It triggers whenever $resize_ratio < .111 (area less than 1/32).
However, whenever the linear downscaling factor is between 3x and 5x:
$resize_ratio < .111is true.- But
5 * $dst_wis larger than the original width! - And
5 * $dst_his larger than the original height!
For the reporter's 102MP image (11656 x 8742):
- When scaling to
2560 x 1920,$dst_w * 5 = 12,800pxand$dst_h * 5 = 9,600px. sampleImage()attempts to upsample the 102MP image into a 123MP intermediate canvas.- This exhausts ImageMagick's cache/memory resources (
ImagickException: cache resources exhausted), returningWP_Error('image_resize_error'). - In
wp_create_image_subsizes(), this error causes the big image scaling step to fail, leaving the full-sized image with zero thumbnails generated.
The Fix
- Guard
sampleImage()so it only runs if both resampled dimensions are strictly smaller than the original:if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) && ( $dst_w * $sample_factor < $this->size['width'] && $dst_h * $sample_factor < $this->size['height'] ) ) { $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); }
- Update
$this->sizeimmediately aftercropImage()incrop()so subsequent operations use the cropped dimensions. - Added dedicated unit test coverage in
tests/phpunit/tests/image/imagickSampleImage.phpverifying thatsampleImageis correctly skipped when sampled dimensions exceed the source image while continuing to accelerate true large downscales.
#8
in reply to: ↑ 7
@
13 days ago
Replying to therssoftware:
I investigated the root cause in
WP_Image_Editor_Imagickand found the issue inthumbnail_image():
if ( is_callable( array( $this->image, 'sampleImage' ) ) ) { $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); $sample_factor = 5; if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) { $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); } }The Cause
The pre-resample step was designed to speed up large reductions by nearest-neighbor sampling to 5x the destination size before running the quality filter. It triggers whenever
$resize_ratio < .111(area less than 1/32).
However, whenever the linear downscaling factor is between 3x and 5x:
$resize_ratio < .111is true.- But
5 * $dst_wis larger than the original width!- And
5 * $dst_his larger than the original height!
This looks like issue #48842.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
screenshot of the file in my Wordpress installation, after upload. I have not disabled scaling and scaling works as expected for smaller images that are e.g. 6k.