Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions src/wp-includes/class-wp-image-editor-gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,31 +435,22 @@ public function rotate( $angle ) {
*
* @param bool $horz Flip along Horizontal Axis.
* @param bool $vert Flip along Vertical Axis.
* @return true|WP_Error
* @return bool|WP_Error

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we preserve the true|WP_Error return contract here?

imageflip() returns bool, so returning it directly means this method can now return false when the GD operation fails. The parent WP_Image_Editor::flip() contract is true|WP_Error, and the Imagick implementation also converts failures to WP_Error.

I think we should keep @return true|WP_Error and convert a false result from imageflip() into the existing image_flip_error WP_Error, e.g.:

$result = imageflip( ... );

if ( $result ) {
	return true;
}

return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );

*/
public function flip( $horz, $vert ) {
$w = $this->size['width'];
$h = $this->size['height'];
$dst = wp_imagecreatetruecolor( $w, $h );

if ( is_gd_image( $dst ) ) {
$sx = $vert ? ( $w - 1 ) : 0;
$sy = $horz ? ( $h - 1 ) : 0;
$sw = $vert ? -$w : $w;
$sh = $horz ? -$h : $h;

if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
imagedestroy( $this->image );
}

$this->image = $dst;
if ( ! is_gd_image( $this->image ) ) {
return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
}

return true;
}
if ( $horz && $vert ) {
return imageflip( $this->image, IMG_FLIP_BOTH );
} elseif ( $horz ) {
return imageflip( $this->image, IMG_FLIP_VERTICAL );
} elseif ( $vert ) {
return imageflip( $this->image, IMG_FLIP_HORIZONTAL );
}

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

/**
Expand Down
Loading