Preserve alpha transparency when rotating a PNG while GD is the active image editor.

Adds unit tests.

Props frankpw, voldemortensen.
Fixes #30596.


git-svn-id: https://develop.svn.wordpress.org/trunk@31040 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-01-03 22:01:54 +00:00
parent 6d858cc6e2
commit 3d3e1fb72a
3 changed files with 52 additions and 1 deletions

View File

@ -306,9 +306,12 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
*/
public function rotate( $angle ) {
if ( function_exists('imagerotate') ) {
$rotated = imagerotate( $this->image, $angle, 0 );
$transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 );
$rotated = imagerotate( $this->image, $angle, $transparency );
if ( is_resource( $rotated ) ) {
imagealphablending( $rotated, true );
imagesavealpha( $rotated, true );
imagedestroy( $this->image );
$this->image = $rotated;
$this->update_size();

View File

@ -499,4 +499,28 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
unlink( $save_to_file );
}
/**
*
* @ticket 30596
*/
public function test_image_preserves_alpha_on_rotate() {
$file = DIR_TESTDATA . '/images/transparent.png';
$image = imagecreatefrompng( $file );
$rgb = imagecolorat( $image, 0, 0 );
$expected = imagecolorsforindex( $image, $rgb );
$editor = new WP_Image_Editor_GD( $file );
$this->assertNotInstanceOf( 'WP_Error', $editor );
$editor->load();
$editor->rotate( 180 );
$save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
$editor->save( $save_to_file );
$this->assertImageAlphaAtPointGD( $save_to_file, array( 0,0 ), $expected['alpha'] );
unlink( $save_to_file );
}
}

View File

@ -507,4 +507,28 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
unlink( $save_to_file );
}
/**
*
* @ticket 30596
*/
public function test_image_peserves_alpha_on_rotate() {
$file = DIR_TESTDATA . '/images/transparent.png';
$pre_rotate_editor = new Imagick( $file );
$pre_rotate_pixel = $pre_rotate_editor->getImagePixelColor( 0, 0 );
$pre_rotate_alpha = $pre_rotate_pixel->getColorValue( imagick::COLOR_ALPHA );
$save_to_file = tempnam( get_temp_dir(),'' ) . '.png';
$pre_rotate_editor->writeImage( $save_to_file );
$pre_rotate_editor->destroy();
$image_editor = new WP_Image_Editor_Imagick( $save_to_file );
$image_editor->load();
$this->assertNotInstanceOf( 'WP_Error', $image_editor );
$image_editor->rotate( 180 );
$image_editor->save( $save_to_file );
$this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $pre_rotate_alpha );
unlink( $save_to_file );
}
}