#23039 closed defect (bug) (fixed)
WP_Image_Editor_GD does not preserve alpha if is not resized
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.6 |
| Component: | Media | Version: | 3.5 |
| Severity: | normal | Keywords: | |
| 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)
comment:1
SergeyBiryukov
— 6 months ago
comment:2
markoheijnen
— 6 months ago
It's the same. I guess we can close #22878 as duplicate of this one.
comment:3
joehoyle
— 6 months 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
comment:4
SergeyBiryukov
— 6 months ago
#22878 was marked as a duplicate.
comment:5
SergeyBiryukov
— 6 months ago
- Milestone changed from Awaiting Review to 3.5.1
comment:6
markoheijnen
— 6 months 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
comment:7
joehoyle
— 6 months 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.
comment:8
markoheijnen
— 6 months ago
Yeah that doesn't make sense. Patch is good but you can remove the is_resource() check. It already happens above it.
comment:9
markoheijnen
— 6 months ago
- Keywords has-patch added
comment:10
joehoyle
— 6 months ago
In 1182/tests:
comment:11
nacin
— 6 months ago
- Keywords commit added
comment:12
nacin
— 6 months 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.
comment:13
joehoyle
— 6 months 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
comment:14
ryan
— 3 months ago
Patch still good and ready to go?
comment:15
markoheijnen
— 3 months ago
Yes, please do.
comment:16
follow-up:
↓ 17
dh-shredder
— 3 months 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.
comment:17
in reply to:
↑ 16
SergeyBiryukov
— 3 months 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
comment:18
ryan
— 3 months ago
In 23884:
comment:19
follow-up:
↓ 20
markoheijnen
— 2 months ago
Can we close this ticket as fixed?
comment:20
in reply to:
↑ 19
dh-shredder
— 2 months ago
Replying to markoheijnen:
Can we close this ticket as fixed?
Since this ticket refers to alpha loss, I'd say yes. If we decide to handle GIF better with GD at some point, that can be tracked in #13461
comment:21
dh-shredder
— 2 months ago
- Keywords close added
- Resolution set to fixed
- Status changed from new to closed
comment:22
dh-shredder
— 2 months ago
- Keywords has-patch commit removed
comment:23
dh-shredder
— 2 months ago
- Keywords close removed
Sounds like the cause of #22878.