From: Christoph M. Becker Date: Thu, 14 Nov 2019 08:46:43 +0000 (+0100) Subject: Make the $num_points parameter of php_imagepolygon optional X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2de79f085502315d1f53822eee54e9f39dd809f6;p=php Make the $num_points parameter of php_imagepolygon optional That parameter is mostly useless in practise, and likely has been directly ported from the underlying `gdImagePolygon()` and friends, which require that parameter since the number of elements of the point array would otherwise be unknown. Typical usages of `imagepolygon()`, `imageopenpolygon()` and `imagefilledpolygon()` pass `count($points)/2` or hard-code this value as literal. Since explicitly specifying this parameter is annoying and error-prone, we offer the possibility to omit it, in which case the `$points` array must have an even number of elements, and the number of points is calculated as `count($points)/2`. --- diff --git a/NEWS b/NEWS index 3b85ad8773..ee10d1cb52 100644 --- a/NEWS +++ b/NEWS @@ -19,6 +19,7 @@ PHP NEWS - GD: . Fixed bug #55005 (imagepolygon num_points requirement). (cmb) . Replaced gd resources with objects. (Mark Randall) + . Made the $num_points parameter of php_imagepolygon optional. (cmb) . Removed deprecated image2wbmp(). (cmb) . Removed deprecated png2wbmp() and jpeg2wbmp(). (cmb) diff --git a/UPGRADING b/UPGRADING index 1bb6d93574..431d7528b3 100644 --- a/UPGRADING +++ b/UPGRADING @@ -355,6 +355,12 @@ PHP 8.0 UPGRADE NOTES 9. Other Changes to Extensions ======================================== +- GD: + . The $num_points parameter of imagepolygon(), imageopenpolygon() and + imagefilledpolygon() is now optional, i.e. these functions may be called + with either 3 or 4 arguments. If the arguments is omitted, it is calculated + as count($points)/2. + ======================================== 10. New Global Constants ======================================== diff --git a/ext/gd/gd.c b/ext/gd/gd.c index cc74727420..543e08466c 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -2735,9 +2735,18 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled) gdPointPtr points; int npoints, col, nelem, i; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oall", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL) == FAILURE) { return; } + if (ZEND_NUM_ARGS() == 3) { + COL = NPOINTS; + NPOINTS = zend_hash_num_elements(Z_ARRVAL_P(POINTS)); + if (NPOINTS % 2 != 0) { + zend_value_error("Points array must have an even number of elements"); + return; + } + NPOINTS /= 2; + } im = php_gd_libgdimageptr_from_zval_p(IM); diff --git a/ext/gd/gd.stub.php b/ext/gd/gd.stub.php index ae832648c0..5c54c08bc0 100644 --- a/ext/gd/gd.stub.php +++ b/ext/gd/gd.stub.php @@ -186,11 +186,11 @@ function imagecolortransparent(GdImage $im, int $col = UNKNOWN): ?int {} function imageinterlace(GdImage $im, int $interlace = UNKNOWN): ?int {} -function imagepolygon(GdImage $im, array $points, int $num_pos, int $col): bool {} +function imagepolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {} -function imageopenpolygon(GdImage $im, array $points, int $num_pos, int $col): bool {} +function imageopenpolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {} -function imagefilledpolygon(GdImage $im, array $points, int $num_pos, int $col): bool {} +function imagefilledpolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {} function imagefontwidth(int $font): int {} diff --git a/ext/gd/gd_arginfo.h b/ext/gd/gd_arginfo.h index aac01ed43f..42e6fd699d 100644 --- a/ext/gd/gd_arginfo.h +++ b/ext/gd/gd_arginfo.h @@ -367,10 +367,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imageinterlace, 0, 1, IS_LONG, 1 ZEND_ARG_TYPE_INFO(0, interlace, IS_LONG, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imagepolygon, 0, 4, _IS_BOOL, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imagepolygon, 0, 3, _IS_BOOL, 0) ZEND_ARG_OBJ_INFO(0, im, GdImage, 0) ZEND_ARG_TYPE_INFO(0, points, IS_ARRAY, 0) - ZEND_ARG_TYPE_INFO(0, num_pos, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, num_points_or_col, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, col, IS_LONG, 0) ZEND_END_ARG_INFO() diff --git a/ext/gd/tests/bug43073.phpt b/ext/gd/tests/bug43073.phpt index a8027338ef..c52bdad5be 100644 --- a/ext/gd/tests/bug43073.phpt +++ b/ext/gd/tests/bug43073.phpt @@ -38,7 +38,7 @@ $cos_t = cos(deg2rad($delta_t)); $sin_t = sin(deg2rad($delta_t)); for ($angle = 0.0, $i = 0; $angle < 360.0; $angle += $delta_t, $i++) { $bbox = imagettftext($g, 24, $angle, 400+$x, 400+$y, $black, $font, 'ABCDEF'); - imagepolygon($g, $bbox, 4, $red); + imagepolygon($g, $bbox, $red); printf("%2d: ", $i); for ($j = 0; $j < 8; $j++) { if ($bbox[$j] >= $exp[$i][$j] - 1 && $bbox[$j] <= $exp[$i][$j] + 1) { diff --git a/ext/gd/tests/bug53504.phpt b/ext/gd/tests/bug53504.phpt index 3b2ce4c7eb..c44e4ef0f8 100644 --- a/ext/gd/tests/bug53504.phpt +++ b/ext/gd/tests/bug53504.phpt @@ -72,7 +72,7 @@ foreach ($tests as $testnum => $test) { } // draw bounding box: - imagepolygon($g, $bboxDrawn, 4, $red); + imagepolygon($g, $bboxDrawn, $red); // draw baseline: $width = sqrt(pow($bboxDrawn[2] - $bboxDrawn[0], 2) + pow($bboxDrawn[3] - $bboxDrawn[1], 2)); diff --git a/ext/gd/tests/bug55005.phpt b/ext/gd/tests/bug55005.phpt index a48d92441c..38fb25db07 100644 --- a/ext/gd/tests/bug55005.phpt +++ b/ext/gd/tests/bug55005.phpt @@ -16,6 +16,6 @@ trycatch_dump( fn () => imagepolygon($g, array(200,10, 200,100, 280,100), 2, $fgnd) ); ?> ---EXPECT-- +--EXPECTF-- !! [ValueError] Polygon must have at least 3 points !! [ValueError] Polygon must have at least 3 points diff --git a/ext/gd/tests/bug64641.phpt b/ext/gd/tests/bug64641.phpt index 713daaf85e..2d5768eece 100644 --- a/ext/gd/tests/bug64641.phpt +++ b/ext/gd/tests/bug64641.phpt @@ -18,14 +18,14 @@ $points = array( 100, 200, 100, 300 ); -imagefilledpolygon($im, $points, 3, 0xFFFF00); +imagefilledpolygon($im, $points, 0xFFFF00); $points = array( 300, 200, 400, 200, 500, 200 ); -imagefilledpolygon($im, $points, 3, 0xFFFF00); +imagefilledpolygon($im, $points, 0xFFFF00); $ex = imagecreatefrompng(__DIR__ . '/bug64641.png'); if (($diss = calc_image_dissimilarity($ex, $im)) < 1e-5) { diff --git a/ext/gd/tests/imagefilledpolygon_basic.phpt b/ext/gd/tests/imagefilledpolygon_basic.phpt index 0f5f9cf335..0bede9e6f9 100644 --- a/ext/gd/tests/imagefilledpolygon_basic.phpt +++ b/ext/gd/tests/imagefilledpolygon_basic.phpt @@ -36,7 +36,7 @@ $bg = imagecolorallocate($image, 0, 0, 0); $col_poly = imagecolorallocate($image, 0, 255, 0); // draw the polygon -imagefilledpolygon($image, $points, count($points)/2, $col_poly); +imagefilledpolygon($image, $points, $col_poly); // output the picture to a file imagepng($image, $dest); diff --git a/ext/gd/tests/imageopenpolygon_basic.phpt b/ext/gd/tests/imageopenpolygon_basic.phpt index b6323b4ddc..d25dc99c16 100644 --- a/ext/gd/tests/imageopenpolygon_basic.phpt +++ b/ext/gd/tests/imageopenpolygon_basic.phpt @@ -16,10 +16,10 @@ $green = imagecolorallocate($im, 0, 128, 0); $blue = imagecolorallocate($im, 0, 0, 255); imagefilledrectangle($im, 0,0, 99,99, $white); -imageopenpolygon($im, [10,10, 49,89, 89,10], 3, $black); -imageopenpolygon($im, [10,89, 35,10, 60,89, 85,10], 4, $red); -imageopenpolygon($im, [10,49, 30,89, 50,10, 70,89, 90,10], 5, $green); -imageopenpolygon($im, [10,50, 25,10, 40,89, 55,10, 80,89, 90,50], 6, $blue); +imageopenpolygon($im, [10,10, 49,89, 89,10], $black); +imageopenpolygon($im, [10,89, 35,10, 60,89, 85,10], $red); +imageopenpolygon($im, [10,49, 30,89, 50,10, 70,89, 90,10], $green); +imageopenpolygon($im, [10,50, 25,10, 40,89, 55,10, 80,89, 90,50], $blue); test_image_equals_file(__DIR__ . DIRECTORY_SEPARATOR . 'imageopenpolygon.png', $im); ?> diff --git a/ext/gd/tests/imagepolygon_aa.phpt b/ext/gd/tests/imagepolygon_aa.phpt index e6a7dafd1d..97d607d9c4 100644 --- a/ext/gd/tests/imagepolygon_aa.phpt +++ b/ext/gd/tests/imagepolygon_aa.phpt @@ -14,7 +14,7 @@ $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0,0, 99,99, $white); imageantialias($im, true); -imagepolygon($im, [10,10, 49,89, 89,49], 3, $black); +imagepolygon($im, [10,10, 49,89, 89,49], $black); test_image_equals_file(__DIR__ . DIRECTORY_SEPARATOR . 'imagepolygon_aa.png', $im); ?> diff --git a/ext/gd/tests/imagepolygon_basic.phpt b/ext/gd/tests/imagepolygon_basic.phpt index 5dfecfbf19..4880f3ebe7 100644 --- a/ext/gd/tests/imagepolygon_basic.phpt +++ b/ext/gd/tests/imagepolygon_basic.phpt @@ -33,7 +33,6 @@ imagepolygon($image, array ( 100, 200, 300, 200 ), - 3, $col_poly); // output the picture to a file diff --git a/ext/gd/tests/libgd00100.phpt b/ext/gd/tests/libgd00100.phpt index a0ad74a5f5..c6fa6dd30e 100644 --- a/ext/gd/tests/libgd00100.phpt +++ b/ext/gd/tests/libgd00100.phpt @@ -33,7 +33,7 @@ $points = array( $x+$d, ($top+$bot)/2, $x, $bot ); -imagefilledpolygon($im, $points, 5, $yellow); +imagefilledpolygon($im, $points, $yellow); // left-facing M not on baseline $top = 40; @@ -47,7 +47,7 @@ $points = array( $left, $bot, ($left+$right)/2, ($top+$bot)/2 ); -imagefilledpolygon($im, $points, 5, $purple); +imagefilledpolygon($im, $points, $purple); // left-facing M on baseline $top = 240; @@ -61,7 +61,7 @@ $points = array( $left, $bot, ($left+$right)/2, ($top+$bot)/2 ); -imagefilledpolygon($im, $points, 5, $magenta); +imagefilledpolygon($im, $points, $magenta); // left-facing M on ceiling $top = -15; @@ -75,23 +75,23 @@ $points = array( $left, $bot, ($left+$right)/2, ($top+$bot)/2 ); -imagefilledpolygon($im, $points, 5, $blue); +imagefilledpolygon($im, $points, $blue); $d = 30; $x = 150; $y = 150; $diamond = array($x-$d, $y, $x, $y+$d, $x+$d, $y, $x, $y-$d); -imagefilledpolygon($im, $diamond, 4, $green); +imagefilledpolygon($im, $diamond, $green); $x = 180; $y = 225; $diamond = array($x-$d, $y, $x, $y+$d, $x+$d, $y, $x, $y-$d); -imagefilledpolygon($im, $diamond, 4, $red); +imagefilledpolygon($im, $diamond, $red); $x = 225; $y = 255; $diamond = array($x-$d, $y, $x, $y+$d, $x+$d, $y, $x, $y-$d); -imagefilledpolygon($im, $diamond, 4, $cyan); +imagefilledpolygon($im, $diamond, $cyan); // M (bridge) not touching bottom boundary $top = 100; @@ -104,7 +104,7 @@ $points = array( $x+$d, ($top+$bot)/2, $x, $bot ); -imagefilledpolygon($im, $points, 5, $black); +imagefilledpolygon($im, $points, $black); include_once __DIR__ . '/func.inc'; test_image_equals_file(__DIR__ . '/libgd00100.png', $im);