Make WordPress Core

Changeset 31040


Ignore:
Timestamp:
01/03/2015 10:01:54 PM (9 years ago)
Author:
wonderboymusic
Message:

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

Adds unit tests.

Props frankpw, voldemortensen.
Fixes #30596.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r30681 r31040  
    307307    public function rotate( $angle ) {
    308308        if ( function_exists('imagerotate') ) {
    309             $rotated = imagerotate( $this->image, $angle, 0 );
     309            $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 );
     310            $rotated = imagerotate( $this->image, $angle, $transparency );
    310311
    311312            if ( is_resource( $rotated ) ) {
     313                imagealphablending( $rotated, true );
     314                imagesavealpha( $rotated, true );
    312315                imagedestroy( $this->image );
    313316                $this->image = $rotated;
  • trunk/tests/phpunit/tests/image/editor_gd.php

    r30660 r31040  
    500500        unlink( $save_to_file );
    501501    }
     502
     503    /**
     504     *
     505     * @ticket 30596
     506     */
     507    public function test_image_preserves_alpha_on_rotate() {
     508        $file = DIR_TESTDATA . '/images/transparent.png';
     509
     510        $image = imagecreatefrompng( $file );
     511        $rgb = imagecolorat( $image, 0, 0 );
     512        $expected = imagecolorsforindex( $image, $rgb );
     513
     514        $editor = new WP_Image_Editor_GD( $file );
     515                $this->assertNotInstanceOf( 'WP_Error', $editor );
     516                $editor->load();
     517                $editor->rotate( 180 );
     518                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
     519
     520                $editor->save( $save_to_file );
     521
     522                $this->assertImageAlphaAtPointGD( $save_to_file, array( 0,0 ), $expected['alpha'] );
     523                unlink( $save_to_file );
     524
     525    }
    502526}
  • trunk/tests/phpunit/tests/image/editor_imagick.php

    r30990 r31040  
    508508        unlink( $save_to_file );
    509509    }
     510
     511    /**
     512     *
     513     * @ticket 30596
     514     */
     515    public function test_image_peserves_alpha_on_rotate() {
     516        $file = DIR_TESTDATA . '/images/transparent.png';
     517
     518        $pre_rotate_editor = new Imagick( $file );
     519        $pre_rotate_pixel = $pre_rotate_editor->getImagePixelColor( 0, 0 );
     520        $pre_rotate_alpha = $pre_rotate_pixel->getColorValue( imagick::COLOR_ALPHA );
     521        $save_to_file = tempnam( get_temp_dir(),'' ) . '.png';
     522        $pre_rotate_editor->writeImage( $save_to_file );
     523        $pre_rotate_editor->destroy();
     524
     525        $image_editor = new WP_Image_Editor_Imagick( $save_to_file );
     526        $image_editor->load();
     527        $this->assertNotInstanceOf( 'WP_Error', $image_editor );
     528        $image_editor->rotate( 180 );
     529        $image_editor->save( $save_to_file );
     530
     531        $this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $pre_rotate_alpha );
     532        unlink( $save_to_file );
     533    }
    510534}
Note: See TracChangeset for help on using the changeset viewer.