Skip to content

Commit 8eec1a9

Browse files
RumanCodesclaude
andcommitted
Media: Keep the true|WP_Error contract in WP_Image_Editor_GD::flip().
Converts a failed `imageflip()` call, or a missing GD image, into the existing `image_flip_error` WP_Error instead of returning `false`, matching `WP_Image_Editor::flip()` and the Imagick implementation. Adds tests confirming that every pixel moves to the expected position for each flip direction, and that a failure returns a WP_Error. Props ruman. See #66113. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6a4c704 commit 8eec1a9

2 files changed

Lines changed: 122 additions & 8 deletions

File tree

‎src/wp-includes/class-wp-image-editor-gd.php‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,22 +435,26 @@ public function rotate( $angle ) {
435435
*
436436
* @param bool $horz Flip along Horizontal Axis.
437437
* @param bool $vert Flip along Vertical Axis.
438-
* @return bool|WP_Error
438+
* @return true|WP_Error
439439
*/
440440
public function flip( $horz, $vert ) {
441-
if ( ! is_gd_image( $this->image ) ) {
442-
return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
441+
if ( ! $horz && ! $vert ) {
442+
return true;
443443
}
444444

445445
if ( $horz && $vert ) {
446-
return imageflip( $this->image, IMG_FLIP_BOTH );
446+
$mode = IMG_FLIP_BOTH;
447447
} elseif ( $horz ) {
448-
return imageflip( $this->image, IMG_FLIP_VERTICAL );
449-
} elseif ( $vert ) {
450-
return imageflip( $this->image, IMG_FLIP_HORIZONTAL );
448+
$mode = IMG_FLIP_VERTICAL;
449+
} else {
450+
$mode = IMG_FLIP_HORIZONTAL;
451451
}
452452

453-
return true;
453+
if ( is_gd_image( $this->image ) && imageflip( $this->image, $mode ) ) {
454+
return true;
455+
}
456+
457+
return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
454458
}
455459

456460
/**

‎tests/phpunit/tests/image/editorGd.php‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,116 @@ public function test_flip() {
586586
$this->assertSame( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 0, 99 ) );
587587
}
588588

589+
/**
590+
* Tests that flipping moves every pixel to the expected position.
591+
*
592+
* @ticket 66113
593+
*
594+
* @dataProvider data_flip
595+
*
596+
* @covers WP_Image_Editor_GD::flip
597+
*
598+
* @param bool $horz Whether to flip along the horizontal axis.
599+
* @param bool $vert Whether to flip along the vertical axis.
600+
*/
601+
public function test_flip_moves_pixels( $horz, $vert ) {
602+
$gd_image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/gradient-square.jpg' );
603+
$gd_image_editor->load();
604+
605+
$property = new ReflectionProperty( $gd_image_editor, 'image' );
606+
if ( PHP_VERSION_ID < 80100 ) {
607+
$property->setAccessible( true );
608+
}
609+
610+
// A 3x2 image in which every pixel has a distinct color.
611+
$width = 3;
612+
$height = 2;
613+
$image = imagecreatetruecolor( $width, $height );
614+
for ( $x = 0; $x < $width; $x++ ) {
615+
for ( $y = 0; $y < $height; $y++ ) {
616+
imagesetpixel( $image, $x, $y, ( $x + 1 ) * 0x100000 + ( $y + 1 ) * 0x10 );
617+
}
618+
}
619+
$original = imagecreatetruecolor( $width, $height );
620+
imagecopy( $original, $image, 0, 0, 0, 0, $width, $height );
621+
622+
$property->setValue( $gd_image_editor, $image );
623+
624+
$size_property = new ReflectionProperty( $gd_image_editor, 'size' );
625+
if ( PHP_VERSION_ID < 80100 ) {
626+
$size_property->setAccessible( true );
627+
}
628+
$size_property->setValue(
629+
$gd_image_editor,
630+
array(
631+
'width' => $width,
632+
'height' => $height,
633+
)
634+
);
635+
636+
$this->assertTrue( $gd_image_editor->flip( $horz, $vert ), 'flip() should return true on success.' );
637+
638+
$flipped = $property->getValue( $gd_image_editor );
639+
for ( $x = 0; $x < $width; $x++ ) {
640+
for ( $y = 0; $y < $height; $y++ ) {
641+
$this->assertSame(
642+
imagecolorat( $original, $x, $y ),
643+
imagecolorat( $flipped, $vert ? $width - 1 - $x : $x, $horz ? $height - 1 - $y : $y ),
644+
"Pixel ($x, $y) was not moved to the expected position."
645+
);
646+
}
647+
}
648+
}
649+
650+
/**
651+
* Tests that flipping returns a WP_Error, not false, when the image cannot be flipped.
652+
*
653+
* @ticket 66113
654+
*
655+
* @covers WP_Image_Editor_GD::flip
656+
*/
657+
public function test_flip_returns_wp_error_on_failure() {
658+
$gd_image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/gradient-square.jpg' );
659+
$gd_image_editor->load();
660+
661+
$property = new ReflectionProperty( $gd_image_editor, 'image' );
662+
if ( PHP_VERSION_ID < 80100 ) {
663+
$property->setAccessible( true );
664+
}
665+
$property->setValue( $gd_image_editor, false );
666+
667+
$result = $gd_image_editor->flip( true, false );
668+
669+
$this->assertWPError( $result, 'flip() should return a WP_Error on failure.' );
670+
$this->assertSame( 'image_flip_error', $result->get_error_code(), 'The error code should be image_flip_error.' );
671+
}
672+
673+
/**
674+
* Data provider.
675+
*
676+
* @return array<string, array{ horz: bool, vert: bool }>
677+
*/
678+
public function data_flip() {
679+
return array(
680+
'horizontal axis' => array(
681+
'horz' => true,
682+
'vert' => false,
683+
),
684+
'vertical axis' => array(
685+
'horz' => false,
686+
'vert' => true,
687+
),
688+
'both axes' => array(
689+
'horz' => true,
690+
'vert' => true,
691+
),
692+
'neither axis' => array(
693+
'horz' => false,
694+
'vert' => false,
695+
),
696+
);
697+
}
698+
589699
/**
590700
* Tests that an image created with WP_Image_Editor_GD preserves alpha with no resizing.
591701
*

0 commit comments

Comments
 (0)