#62331 closed defect (bug) (fixed)
Image editing in GD runs image stream functions multiple times
Reported by: | glynnquelch | Owned by: | adamsilverstein |
---|---|---|---|
Milestone: | 6.7.1 | Priority: | normal |
Severity: | normal | Version: | 6.5 |
Component: | Media | Keywords: | has-patch fixed-major commit dev-reviewed |
Focuses: | Cc: |
Description
I referenced this on a similar issue
https://core.trac.wordpress.org/ticket/23127#comment:29
When the imagecreatefromstring() function is called in wp-includes/class-wp-image-editor-gd.php, it will often call this function twice. This often results in serious memory issues for larger images and at some formats it doubles the memory used
<?php // WebP may not work with imagecreatefromstring(). if ( function_exists( 'imagecreatefromwebp' ) && ( 'image/webp' === wp_get_image_mime( $this->file ) ) ) { $this->image = @imagecreatefromwebp( $this->file ); } else { $this->image = @imagecreatefromstring( $file_contents ); } // AVIF may not work with imagecreatefromstring(). if ( function_exists( 'imagecreatefromavif' ) && ( 'image/avif' === wp_get_image_mime( $this->file ) ) ) { $this->image = @imagecreatefromavif( $this->file ); } else { $this->image = @imagecreatefromstring( $file_contents ); }
If I make a change locally to
<?php // WebP may not work with imagecreatefromstring(). if ( function_exists( 'imagecreatefromwebp' ) && ( 'image/webp' === wp_get_image_mime( $this->file ) ) ) { $this->image = @imagecreatefromwebp( $this->file ); // AVIF may not work with imagecreatefromstring(). } elseif ( function_exists( 'imagecreatefromavif' ) && ( 'image/avif' === wp_get_image_mime( $this->file ) ) ) { $this->image = @imagecreatefromavif( $this->file ); } else { $this->image = @imagecreatefromstring( $file_contents ); }
I see a reduction in the memory overhead
Filename | Size (W x H) | Filesize | Current GD Peak Memory | Improved Flow Peak Memory |
---|---|---|---|---|
Suru_Bog_10000px.jpg | 10000 x 4534 | 33.08 MB | 410.41 MB | 266 MB |
test.jpg | 7500 x 7500 | 1.75 MB | 510.02 MB | 308.71 MB |
156442-ultra-widescreen-wallpaper.jpg | 4500 x 1901 | 642.36 KB | 96.98 MB | 92.96 MB |
logo.png | 1024 x 512 | 85.47 KB | 20.36 MB | 10.29 MB |
Sample-10345x5531.webp | 10345 x 3989 | 7.49MB | 378.79 MB | 207.38MB |
minion-large.webp | 10000 x 5000 | 2.15MB | 419.38 MB | 224.05MB |
minion-large.gif | 25000 x 12500 | 25.84MB | 734.04 MB | 424.89MB |
SamplePNGImage_30mbmb.png | 7345 x 2832 | 31.39MB | 297.11 MB | 241.41MB |
IMG_20211115_123549.jpg | 4624 x 3472 | 2.57 MB | 167.69 MB | 132.58MB |
Some smaller images see no difference and some gifs use more memory at times, but for images with larger dimensions, we see memory allocation errors with hosts that limit memory at 512mb of ram.
Attachments (1)
Change History (18)
#1
@
5 weeks ago
- Keywords has-patch needs-testing added; needs-patch removed
- Milestone changed from Awaiting Review to 6.7.1
Thanks for this report, @glynnquelch. And welcome to Trac!
Unfortunately, it’s too late to consider this for 6.7, but this sort of thing would be great to improve as soon as we’re able to validate and test properly.
Moving to 6.7.1 for investigation.
#2
@
5 weeks ago
- Version changed from trunk to 6.5
Good catch @glynnquelch and thanks for the new ticket - the logic there does look incorrect. It was fine when we added the original conditional when WebP was introduced. When we added AVIF support we should added the else logic as you have it. Marking this as having originated in 6.5.
This ticket was mentioned in PR #7705 on WordPress/wordpress-develop by @adamsilverstein.
5 weeks ago
#4
Trac ticket: https://core.trac.wordpress.org/ticket/62331
#5
@
5 weeks ago
Tests for patch running on https://github.com/WordPress/wordpress-develop/pull/7705 (also adds consolidating comment lines).
It would be nice to add a unit test for this, ideally asserting that imagecreatefromstring
is only called once.
This is a pretty significant bug for GD users and a very small change to fix: we may want to consider back porting to 6.5+.
#6
@
5 weeks ago
It would be nice to add a unit test for this, ideally asserting that imagecreatefromstring is only called once.
Not sure this is possible with our testing framework. Fortunately, the fix is clear looking at the code logic. Previous to the this change, imagecreatefromstring
is called twice.
This ticket was mentioned in Slack in #core by desrosj. View the logs.
3 weeks ago
#9
@
3 weeks ago
Because of a few bug reports opened since 6.7 was released, the Core team is evaluating the need for a short 6.7.1 cycle (possibly next week).
While this issue was not introduced during 6.7 (which technically means it now falls outside of the focus for 6.7.1 as currently defined), I think this is worth including. As @adamsilverstein said, this is a pretty significant bug for GD users, and there are likely some undiscovered edge cases from running WebP images through imagecreatefromstring
).
#11
@
3 weeks ago
- Keywords fixed-major added
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening to consider backporting.
Diff to improve the control flow