Make WordPress Core

Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#23039 closed defect (bug) (fixed)

WP_Image_Editor_GD does not preserve alpha if is not resized

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

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

Download all attachments as: .zip

Change History (27)

#1 @SergeyBiryukov
12 years ago

Sounds like the cause of #22878.

#2 @markoheijnen
12 years ago

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

#3 @joehoyle
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

#4 @SergeyBiryukov
12 years ago

#22878 was marked as a duplicate.

#5 @SergeyBiryukov
12 years ago

  • Milestone changed from Awaiting Review to 3.5.1

#6 @markoheijnen
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 @joehoyle
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 @markoheijnen
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.

@joehoyle
12 years ago

Good point, updated

#9 @markoheijnen
12 years ago

  • Keywords has-patch added

#10 @joehoyle
12 years ago

In 1182/tests:

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

#11 @nacin
12 years ago

  • Keywords commit added

@kirasong
12 years ago

Corrected path so that patch applies

#12 @nacin
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 @joehoyle
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

@joehoyle
12 years ago

Added some more tests for WP_Image_Editor_GD

#14 @ryan
11 years ago

Patch still good and ready to go?

#15 @markoheijnen
11 years ago

Yes, please do.

#16 follow-up: @dh-shredder
11 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 @SergeyBiryukov
11 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

#18 @ryan
11 years ago

In 23884:

Preserve alpha in WP_Image_Editor_GD when images are not resized.

Props joehoyle
see #23039

#19 follow-up: @markoheijnen
11 years ago

Can we close this ticket as fixed?

#20 in reply to: ↑ 19 @dh-shredder
11 years 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

#21 @dh-shredder
11 years ago

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

#22 @dh-shredder
11 years ago

  • Keywords has-patch commit removed

#23 @dh-shredder
11 years ago

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