Skip to content

Commit 6a4c704

Browse files
arshidkv12RumanCodes
authored andcommitted
Media: Use imageflip() in WP_Image_Editor_GD::flip().
Replaces the imagecopyresampled() implementation, which relies on negative source dimensions, with imageflip(). The old approach produces incorrect pixel data with PHP 8.6's bundled GD 2.4.0. See #66113.
1 parent 0d859ba commit 6a4c704

1 file changed

Lines changed: 11 additions & 20 deletions

File tree

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -435,31 +435,22 @@ public function rotate( $angle ) {
435435
*
436436
* @param bool $horz Flip along Horizontal Axis.
437437
* @param bool $vert Flip along Vertical Axis.
438-
* @return true|WP_Error
438+
* @return bool|WP_Error
439439
*/
440440
public function flip( $horz, $vert ) {
441-
$w = $this->size['width'];
442-
$h = $this->size['height'];
443-
$dst = wp_imagecreatetruecolor( $w, $h );
444-
445-
if ( is_gd_image( $dst ) ) {
446-
$sx = $vert ? ( $w - 1 ) : 0;
447-
$sy = $horz ? ( $h - 1 ) : 0;
448-
$sw = $vert ? -$w : $w;
449-
$sh = $horz ? -$h : $h;
450-
451-
if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
452-
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
453-
imagedestroy( $this->image );
454-
}
455-
456-
$this->image = $dst;
441+
if ( ! is_gd_image( $this->image ) ) {
442+
return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
443+
}
457444

458-
return true;
459-
}
445+
if ( $horz && $vert ) {
446+
return imageflip( $this->image, IMG_FLIP_BOTH );
447+
} elseif ( $horz ) {
448+
return imageflip( $this->image, IMG_FLIP_VERTICAL );
449+
} elseif ( $vert ) {
450+
return imageflip( $this->image, IMG_FLIP_HORIZONTAL );
460451
}
461452

462-
return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
453+
return true;
463454
}
464455

465456
/**

0 commit comments

Comments
 (0)