diff --git a/NEWS b/NEWS index b27f23027e9..df7bc70af79 100644 --- a/NEWS +++ b/NEWS @@ -25,6 +25,8 @@ PHP NEWS falling back to IMG_CROP_SIDES. . The default $mode parameter of imagecropauto() has been changed to IMG_CROP_DEFAULT; passing -1 is now deprecated. + . Added support for aspect ratio preserving scaling to a fixed height for + imagescale(). (Andreas Treichel) - Hash: . The hash extension is now an integral part of PHP and cannot be disabled diff --git a/UPGRADING b/UPGRADING index ce157fc702d..2da9ad242b9 100644 --- a/UPGRADING +++ b/UPGRADING @@ -145,6 +145,8 @@ PHP 7.4 UPGRADE NOTES * Threshold-cropping now uses the algorithm of system libgd . The default $mode parameter of imagecropauto() has been changed to IMG_CROP_DEFAULT; passing -1 is now deprecated. + . imagescale() now supports aspect ratio preserving scaling to a fixed height + by passing -1 as $new_width. - Hash: . The hash extension cannot be disabled anymore and is always an integral diff --git a/ext/gd/gd.c b/ext/gd/gd.c index f4b7d2c2a5b..d4e4db1be6b 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -4757,15 +4757,19 @@ PHP_FUNCTION(imagescale) RETURN_FALSE; } - if (tmp_h < 0) { + if (tmp_h < 0 || tmp_w < 0) { /* preserve ratio */ long src_x, src_y; src_x = gdImageSX(im); src_y = gdImageSY(im); - if (src_x) { + + if (src_x && tmp_h < 0) { tmp_h = tmp_w * src_y / src_x; } + if (src_y && tmp_w < 0) { + tmp_w = tmp_h * src_x / src_y; + } } if (tmp_h <= 0 || tmp_h > INT_MAX || tmp_w <= 0 || tmp_w > INT_MAX) { diff --git a/ext/gd/tests/imagescale_preserve_ratio.phpt b/ext/gd/tests/imagescale_preserve_ratio.phpt new file mode 100644 index 00000000000..00076e431e5 --- /dev/null +++ b/ext/gd/tests/imagescale_preserve_ratio.phpt @@ -0,0 +1,55 @@ +--TEST-- +Scale images and preserve aspect ratio +--SKIPIF-- + +--FILE-- + +DONE +--EXPECT-- +int(64) +int(96) +int(42) +int(64) +int(64) +int(42) +int(96) +int(64) +int(64) +int(64) +int(64) +int(64) +DONE