mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-04 14:47:02 +00:00
Media: Return WP_Error when cropping with bad input to avoid fatal.
This avoids an error on PHP 8 caused by calling `wp_imagecreatetruecolor()` with inputs that aren't numeric, or are less than 0. Props hellofromtonya, Boniu91, metalandcoffee, SergeyBiryukov. Fixes #51937. git-svn-id: https://develop.svn.wordpress.org/trunk@49751 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -405,25 +405,128 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test cropping an image
|
||||
* Test cropping an image.
|
||||
*
|
||||
* @ticket 51937
|
||||
*
|
||||
* @dataProvider data_crop
|
||||
*/
|
||||
public function test_crop() {
|
||||
public function test_crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
|
||||
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
|
||||
|
||||
$gd_image_editor = new WP_Image_Editor_GD( $file );
|
||||
$gd_image_editor->load();
|
||||
|
||||
$gd_image_editor->crop( 0, 0, 50, 50 );
|
||||
$gd_image_editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
|
||||
|
||||
$this->assertSame(
|
||||
array(
|
||||
'width' => 50,
|
||||
'height' => 50,
|
||||
'width' => (int) $src_w,
|
||||
'height' => (int) $src_h,
|
||||
),
|
||||
$gd_image_editor->get_size()
|
||||
);
|
||||
}
|
||||
|
||||
public function data_crop() {
|
||||
return array(
|
||||
'src height and width must be greater than 0' => array(
|
||||
'src_x' => 0,
|
||||
'src_y' => 0,
|
||||
'src_w' => 50,
|
||||
'src_h' => 50,
|
||||
),
|
||||
'src height and width can be string but must be greater than 0' => array(
|
||||
'src_x' => 10,
|
||||
'src_y' => '10',
|
||||
'src_w' => '50',
|
||||
'src_h' => '50',
|
||||
),
|
||||
'dst height and width must be greater than 0' => array(
|
||||
'src_x' => 10,
|
||||
'src_y' => '10',
|
||||
'src_w' => 150,
|
||||
'src_h' => 150,
|
||||
'dst_w' => 150,
|
||||
'dst_h' => 150,
|
||||
),
|
||||
'dst height and width can be string but must be greater than 0' => array(
|
||||
'src_x' => 10,
|
||||
'src_y' => '10',
|
||||
'src_w' => 150,
|
||||
'src_h' => 150,
|
||||
'dst_w' => '150',
|
||||
'dst_h' => '150',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test should return WP_Error when dimensions are not integer or are <= 0.
|
||||
*
|
||||
* @ticket 51937
|
||||
*
|
||||
* @dataProvider data_crop_invalid_dimensions
|
||||
*/
|
||||
public function test_crop_invalid_dimensions( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
|
||||
$file = DIR_TESTDATA . '/images/gradient-square.jpg';
|
||||
|
||||
$gd_image_editor = new WP_Image_Editor_GD( $file );
|
||||
$gd_image_editor->load();
|
||||
|
||||
$actual = $gd_image_editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
|
||||
|
||||
$this->assertInstanceOf( 'WP_Error', $actual );
|
||||
$this->assertSame( 'image_crop_error', $actual->get_error_code() );
|
||||
}
|
||||
|
||||
public function data_crop_invalid_dimensions() {
|
||||
return array(
|
||||
'src height must be greater than 0' => array(
|
||||
'src_x' => 0,
|
||||
'src_y' => 0,
|
||||
'src_w' => 100,
|
||||
'src_h' => 0,
|
||||
),
|
||||
'src width must be greater than 0' => array(
|
||||
'src_x' => 10,
|
||||
'src_y' => '10',
|
||||
'src_w' => 0,
|
||||
'src_h' => 100,
|
||||
),
|
||||
'src height must be numeric and greater than 0' => array(
|
||||
'src_x' => 10,
|
||||
'src_y' => '10',
|
||||
'src_w' => 100,
|
||||
'src_h' => 'NaN',
|
||||
),
|
||||
'dst height must be numeric and greater than 0' => array(
|
||||
'src_x' => 0,
|
||||
'src_y' => 0,
|
||||
'src_w' => 100,
|
||||
'src_h' => 50,
|
||||
'dst_w' => '100',
|
||||
'dst_h' => 'NaN',
|
||||
),
|
||||
'src and dst height and width must be greater than 0' => array(
|
||||
'src_x' => 0,
|
||||
'src_y' => 0,
|
||||
'src_w' => 0,
|
||||
'src_h' => 0,
|
||||
'dst_w' => 0,
|
||||
'dst_h' => 0,
|
||||
),
|
||||
'src and dst height and width can be string but must be greater than 0' => array(
|
||||
'src_x' => 0,
|
||||
'src_y' => 0,
|
||||
'src_w' => '0',
|
||||
'src_h' => '0',
|
||||
'dst_w' => '0',
|
||||
'dst_h' => '0',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rotating an image 180 deg
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user