Skip to content

Commit 1b65126

Browse files
RumanCodesclaude
andcommitted
Media: Use imageflip() in the deprecated _flip_image_resource().
`_flip_image_resource()` used the same `imagecopyresampled()` approach with negative source dimensions as `WP_Image_Editor_GD::flip()`, which produces a blank image with PHP 8.6's bundled GD 2.4.0. It now uses `imageflip()` as well, and returns the image unchanged when it is not a GD image or no axis is given. Props ruman. See #66113. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8eec1a9 commit 1b65126

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

‎src/wp-admin/includes/image-edit.php‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -577,25 +577,20 @@ function _rotate_image_resource( $img, $angle ) {
577577
function _flip_image_resource( $img, $horz, $vert ) {
578578
_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
579579

580-
$w = imagesx( $img );
581-
$h = imagesy( $img );
582-
$dst = wp_imagecreatetruecolor( $w, $h );
583-
584-
if ( is_gd_image( $dst ) ) {
585-
$sx = $vert ? ( $w - 1 ) : 0;
586-
$sy = $horz ? ( $h - 1 ) : 0;
587-
$sw = $vert ? -$w : $w;
588-
$sh = $horz ? -$h : $h;
589-
590-
if ( imagecopyresampled( $dst, $img, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
591-
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
592-
imagedestroy( $img );
593-
}
580+
if ( ! is_gd_image( $img ) || ( ! $horz && ! $vert ) ) {
581+
return $img;
582+
}
594583

595-
$img = $dst;
596-
}
584+
if ( $horz && $vert ) {
585+
$mode = IMG_FLIP_BOTH;
586+
} elseif ( $horz ) {
587+
$mode = IMG_FLIP_VERTICAL;
588+
} else {
589+
$mode = IMG_FLIP_HORIZONTAL;
597590
}
598591

592+
imageflip( $img, $mode );
593+
599594
return $img;
600595
}
601596

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,49 @@ public function test_flip_moves_pixels( $horz, $vert ) {
647647
}
648648
}
649649

650+
/**
651+
* Tests that the deprecated _flip_image_resource() moves every pixel to the expected position.
652+
*
653+
* @ticket 66113
654+
*
655+
* @dataProvider data_flip
656+
*
657+
* @covers ::_flip_image_resource
658+
*
659+
* @expectedDeprecated _flip_image_resource
660+
*
661+
* @param bool $horz Whether to flip along the horizontal axis.
662+
* @param bool $vert Whether to flip along the vertical axis.
663+
*/
664+
public function test_flip_image_resource_moves_pixels( $horz, $vert ) {
665+
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
666+
667+
// A 3x2 image in which every pixel has a distinct color.
668+
$width = 3;
669+
$height = 2;
670+
$image = imagecreatetruecolor( $width, $height );
671+
for ( $x = 0; $x < $width; $x++ ) {
672+
for ( $y = 0; $y < $height; $y++ ) {
673+
imagesetpixel( $image, $x, $y, ( $x + 1 ) * 0x100000 + ( $y + 1 ) * 0x10 );
674+
}
675+
}
676+
$original = imagecreatetruecolor( $width, $height );
677+
imagecopy( $original, $image, 0, 0, 0, 0, $width, $height );
678+
679+
$flipped = _flip_image_resource( $image, $horz, $vert );
680+
681+
$this->assertTrue( is_gd_image( $flipped ), '_flip_image_resource() should return a GD image.' );
682+
for ( $x = 0; $x < $width; $x++ ) {
683+
for ( $y = 0; $y < $height; $y++ ) {
684+
$this->assertSame(
685+
imagecolorat( $original, $x, $y ),
686+
imagecolorat( $flipped, $vert ? $width - 1 - $x : $x, $horz ? $height - 1 - $y : $y ),
687+
"Pixel ($x, $y) was not moved to the expected position."
688+
);
689+
}
690+
}
691+
}
692+
650693
/**
651694
* Tests that flipping returns a WP_Error, not false, when the image cannot be flipped.
652695
*

0 commit comments

Comments
 (0)