WordPress.org

Make WordPress Core

Opened 6 months ago

Closed 2 months ago

Last modified 2 months ago

#23039 closed defect (bug) (fixed)

WP_Image_Editor_GD does not preserve alpha if is not resized

Reported by: joehoyle 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)

wp-image-editor-gd-tests.diff (2.3 KB) - added by joehoyle 6 months ago.
wp-image-editor-gd.diff (669 bytes) - added by joehoyle 6 months ago.
Good point, updated
23039.diff (639 bytes) - added by DH-Shredder 6 months ago.
Corrected path so that patch applies
wp-image-editor-gd-more.diff (3.2 KB) - added by joehoyle 5 months ago.
Added some more tests for WP_Image_Editor_GD

Download all attachments as: .zip

Change History (27)

comment:1 SergeyBiryukov6 months ago

Sounds like the cause of #22878.

comment:2 markoheijnen6 months ago

It's the same. I guess we can close #22878 as duplicate of this one.

comment:3 joehoyle6 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 SergeyBiryukov6 months ago

#22878 was marked as a duplicate.

comment:5 SergeyBiryukov6 months ago

  • Milestone changed from Awaiting Review to 3.5.1

comment:6 markoheijnen6 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 joehoyle6 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 markoheijnen6 months ago

Yeah that doesn't make sense. Patch is good but you can remove the is_resource() check. It already happens above it.

joehoyle6 months ago

Good point, updated

comment:9 markoheijnen6 months ago

  • Keywords has-patch added

comment:10 joehoyle6 months ago

In 1182/tests:

Add some tests for WP_Image_Editor_GD, specifically testing image alpha testing. see #23039

comment:11 nacin6 months ago

  • Keywords commit added

DH-Shredder6 months ago

Corrected path so that patch applies

comment:12 nacin6 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 joehoyle6 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

joehoyle5 months ago

Added some more tests for WP_Image_Editor_GD

comment:14 ryan3 months ago

Patch still good and ready to go?

comment:15 markoheijnen3 months ago

Yes, please do.

comment:16 follow-up: dh-shredder3 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 SergeyBiryukov3 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 ryan3 months ago

In 23884:

Preserve alpha in WP_Image_Editor_GD when images are not resized.

Props joehoyle
see #23039

comment:19 follow-up: markoheijnen2 months ago

Can we close this ticket as fixed?

comment:20 in reply to: ↑ 19 dh-shredder2 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-shredder2 months ago

  • Keywords close added
  • Resolution set to fixed
  • Status changed from new to closed

comment:22 dh-shredder2 months ago

  • Keywords has-patch commit removed

comment:23 dh-shredder2 months ago

  • Keywords close removed
Note: See TracTickets for help on using tickets.