Skip to content

Commit

Permalink
Media: Fix implicit conversion from float to int in image cropping.
Browse files Browse the repository at this point in the history
Cast crop values to integers to prevent PHP error caused by implicit conversion from `float` to `int` values when cropping images using ImageMagick.

Props skithund, mai21, nicomollet, amanias1977, joedolson.
Fixes #59782.

git-svn-id: https://develop.svn.wordpress.org/trunk@58457 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
joedolson committed Jun 21, 2024
1 parent 25e7dc6 commit 4f175e1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/wp-admin/includes/image-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ function image_edit_apply_changes( $image, $changes ) {
$h = $size['height'];

$scale = isset( $sel->r ) ? $sel->r : 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling.
$image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
$image->crop( (int) ( $sel->x * $scale ), (int) ( $sel->y * $scale ), (int) ( $sel->w * $scale ), (int) ( $sel->h * $scale ) );
} else {
$scale = isset( $sel->r ) ? $sel->r : 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling.
$image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,10 @@ public function edit_media_item( $request ) {
case 'crop':
$size = $image_editor->get_size();

$crop_x = round( ( $size['width'] * $args['left'] ) / 100.0 );
$crop_y = round( ( $size['height'] * $args['top'] ) / 100.0 );
$width = round( ( $size['width'] * $args['width'] ) / 100.0 );
$height = round( ( $size['height'] * $args['height'] ) / 100.0 );
$crop_x = (int) round( ( $size['width'] * $args['left'] ) / 100.0 );
$crop_y = (int) round( ( $size['height'] * $args['top'] ) / 100.0 );
$width = (int) round( ( $size['width'] * $args['width'] ) / 100.0 );
$height = (int) round( ( $size['height'] * $args['height'] ) / 100.0 );

if ( $size['width'] !== $width && $size['height'] !== $height ) {
$result = $image_editor->crop( $crop_x, $crop_y, $width, $height );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2376,7 +2376,7 @@ public function test_edit_image_crop() {

$this->assertCount( 1, WP_Image_Editor_Mock::$spy['crop'] );
$this->assertSame(
array( 320.0, 48.0, 64.0, 24.0 ),
array( 320, 48, 64, 24 ),
WP_Image_Editor_Mock::$spy['crop'][0]
);
}
Expand Down

0 comments on commit 4f175e1

Please sign in to comment.