#23039 closed defect (bug) (fixed)
WP_Image_Editor_GD does not preserve alpha if is not resized
Reported by: | joehoyle | Owned by: | |
---|---|---|---|
Milestone: | 3.6 | Priority: | normal |
Severity: | normal | Version: | 3.5 |
Component: | Media | Keywords: | |
Focuses: | Cc: |
Description
If I do something like:
$wp_image_editor_gd->load( $file ) /* $file is a transparent 5px x 5px image) */ $wp_image_editor_gd->resize( 5, 5 ); /* or don't even call resize at all */ $wp_image_editor_gd->save( $new_filepath );
Then the saved file will be stripped of its transparency. This is because for image alpha to be preserved, the GD resource has to be passed to wp_imagecreatetruecolor
and it is only passed to that is the resize is smaller is not equal to the original dimensions. The same goes for crop() etc.
It's best to see this here: http://core.trac.wordpress.org/browser/trunk/wp-includes/class-wp-image-editor-gd.php#L137 , the alpha only preserved it it hits http://core.trac.wordpress.org/browser/trunk/wp-includes/class-wp-image-editor-gd.php#L161
If this is not a known issue I can write a patch and unit tests for it.
Attachments (4)
Change History (27)
#3
@
12 years ago
Ok, I can write a patch, would it be best to just pass $this->image
through imagealphablending($img, false); imagesavealpha($img, true);
on http://core.trac.wordpress.org/browser/trunk/wp-includes/class-wp-image-editor-gd.php#L91
#6
@
12 years ago
Best is to just use wp_imagecreatetruecolor(). We will deprecate it in 3.6 tho and include the code in WP_Image_Editor_GD. We probably should have done that in 3.5
#7
@
12 years ago
I did try and use wp_imagecreatetruecolor
, however that creates an empty gd canvas, so we would need to get width / height of the original, then copy it over to the new wp_imagecreatetruecolor
canvas. That works ok fo resize etc as it's being copied from resource to resource, however on load I thought it was maybe more complicated that just calling on it directly was simpler.
I also wrote some basic unti tests for this, test_image_preserves_alpha
passes after applying patch.
#8
@
12 years ago
Yeah that doesn't make sense. Patch is good but you can remove the is_resource() check. It already happens above it.
#10
@
12 years ago
In 1182/tests:
#12
@
12 years ago
- Milestone changed from 3.5.1 to 3.6
According to IRC, seems like this is not a regression, so moving to 3.6.
#13
@
12 years ago
Good point, I tested under 3.4.2 again, the issue is there too, though the alpha is not lost until you Save the image, whereas in 3.5 the black appears as soon as you click Edit - hence I thought it was a regression
#16
follow-up:
↓ 17
@
12 years ago
This doesn't solve all issues GD has with transparent images, but does make the support better than it is currently for PNGs.
Since this doesn't solve anything for GIF transparency issues and GD, we could still consider a fix/workaround for that in the future.
#17
in reply to:
↑ 16
@
12 years ago
Replying to dh-shredder:
Since this doesn't solve anything for GIF transparency issues and GD, we could still consider a fix/workaround for that in the future.
Related: #13461
Sounds like the cause of #22878.