Skip to content

Commit 2de79f0

Browse files
committed
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`.
1 parent d65e59d commit 2de79f0

14 files changed

+41
-26
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ PHP NEWS
1919
- GD:
2020
. Fixed bug #55005 (imagepolygon num_points requirement). (cmb)
2121
. Replaced gd resources with objects. (Mark Randall)
22+
. Made the $num_points parameter of php_imagepolygon optional. (cmb)
2223
. Removed deprecated image2wbmp(). (cmb)
2324
. Removed deprecated png2wbmp() and jpeg2wbmp(). (cmb)
2425

UPGRADING

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ PHP 8.0 UPGRADE NOTES
355355
9. Other Changes to Extensions
356356
========================================
357357

358+
- GD:
359+
. The $num_points parameter of imagepolygon(), imageopenpolygon() and
360+
imagefilledpolygon() is now optional, i.e. these functions may be called
361+
with either 3 or 4 arguments. If the arguments is omitted, it is calculated
362+
as count($points)/2.
363+
358364
========================================
359365
10. New Global Constants
360366
========================================

ext/gd/gd.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2735,9 +2735,18 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
27352735
gdPointPtr points;
27362736
int npoints, col, nelem, i;
27372737

2738-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oall", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL) == FAILURE) {
2738+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL) == FAILURE) {
27392739
return;
27402740
}
2741+
if (ZEND_NUM_ARGS() == 3) {
2742+
COL = NPOINTS;
2743+
NPOINTS = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
2744+
if (NPOINTS % 2 != 0) {
2745+
zend_value_error("Points array must have an even number of elements");
2746+
return;
2747+
}
2748+
NPOINTS /= 2;
2749+
}
27412750

27422751
im = php_gd_libgdimageptr_from_zval_p(IM);
27432752

ext/gd/gd.stub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ function imagecolortransparent(GdImage $im, int $col = UNKNOWN): ?int {}
186186

187187
function imageinterlace(GdImage $im, int $interlace = UNKNOWN): ?int {}
188188

189-
function imagepolygon(GdImage $im, array $points, int $num_pos, int $col): bool {}
189+
function imagepolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {}
190190

191-
function imageopenpolygon(GdImage $im, array $points, int $num_pos, int $col): bool {}
191+
function imageopenpolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {}
192192

193-
function imagefilledpolygon(GdImage $im, array $points, int $num_pos, int $col): bool {}
193+
function imagefilledpolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {}
194194

195195
function imagefontwidth(int $font): int {}
196196

ext/gd/gd_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imageinterlace, 0, 1, IS_LONG, 1
367367
ZEND_ARG_TYPE_INFO(0, interlace, IS_LONG, 0)
368368
ZEND_END_ARG_INFO()
369369

370-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imagepolygon, 0, 4, _IS_BOOL, 0)
370+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imagepolygon, 0, 3, _IS_BOOL, 0)
371371
ZEND_ARG_OBJ_INFO(0, im, GdImage, 0)
372372
ZEND_ARG_TYPE_INFO(0, points, IS_ARRAY, 0)
373-
ZEND_ARG_TYPE_INFO(0, num_pos, IS_LONG, 0)
373+
ZEND_ARG_TYPE_INFO(0, num_points_or_col, IS_LONG, 0)
374374
ZEND_ARG_TYPE_INFO(0, col, IS_LONG, 0)
375375
ZEND_END_ARG_INFO()
376376

ext/gd/tests/bug43073.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $cos_t = cos(deg2rad($delta_t));
3838
$sin_t = sin(deg2rad($delta_t));
3939
for ($angle = 0.0, $i = 0; $angle < 360.0; $angle += $delta_t, $i++) {
4040
$bbox = imagettftext($g, 24, $angle, 400+$x, 400+$y, $black, $font, 'ABCDEF');
41-
imagepolygon($g, $bbox, 4, $red);
41+
imagepolygon($g, $bbox, $red);
4242
printf("%2d: ", $i);
4343
for ($j = 0; $j < 8; $j++) {
4444
if ($bbox[$j] >= $exp[$i][$j] - 1 && $bbox[$j] <= $exp[$i][$j] + 1) {

ext/gd/tests/bug53504.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ foreach ($tests as $testnum => $test) {
7272
}
7373

7474
// draw bounding box:
75-
imagepolygon($g, $bboxDrawn, 4, $red);
75+
imagepolygon($g, $bboxDrawn, $red);
7676

7777
// draw baseline:
7878
$width = sqrt(pow($bboxDrawn[2] - $bboxDrawn[0], 2) + pow($bboxDrawn[3] - $bboxDrawn[1], 2));

ext/gd/tests/bug55005.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ trycatch_dump(
1616
fn () => imagepolygon($g, array(200,10, 200,100, 280,100), 2, $fgnd)
1717
);
1818
?>
19-
--EXPECT--
19+
--EXPECTF--
2020
!! [ValueError] Polygon must have at least 3 points
2121
!! [ValueError] Polygon must have at least 3 points

ext/gd/tests/bug64641.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ $points = array(
1818
100, 200,
1919
100, 300
2020
);
21-
imagefilledpolygon($im, $points, 3, 0xFFFF00);
21+
imagefilledpolygon($im, $points, 0xFFFF00);
2222

2323
$points = array(
2424
300, 200,
2525
400, 200,
2626
500, 200
2727
);
28-
imagefilledpolygon($im, $points, 3, 0xFFFF00);
28+
imagefilledpolygon($im, $points, 0xFFFF00);
2929

3030
$ex = imagecreatefrompng(__DIR__ . '/bug64641.png');
3131
if (($diss = calc_image_dissimilarity($ex, $im)) < 1e-5) {

ext/gd/tests/imagefilledpolygon_basic.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ $bg = imagecolorallocate($image, 0, 0, 0);
3636
$col_poly = imagecolorallocate($image, 0, 255, 0);
3737

3838
// draw the polygon
39-
imagefilledpolygon($image, $points, count($points)/2, $col_poly);
39+
imagefilledpolygon($image, $points, $col_poly);
4040

4141
// output the picture to a file
4242
imagepng($image, $dest);

ext/gd/tests/imageopenpolygon_basic.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ $green = imagecolorallocate($im, 0, 128, 0);
1616
$blue = imagecolorallocate($im, 0, 0, 255);
1717
imagefilledrectangle($im, 0,0, 99,99, $white);
1818

19-
imageopenpolygon($im, [10,10, 49,89, 89,10], 3, $black);
20-
imageopenpolygon($im, [10,89, 35,10, 60,89, 85,10], 4, $red);
21-
imageopenpolygon($im, [10,49, 30,89, 50,10, 70,89, 90,10], 5, $green);
22-
imageopenpolygon($im, [10,50, 25,10, 40,89, 55,10, 80,89, 90,50], 6, $blue);
19+
imageopenpolygon($im, [10,10, 49,89, 89,10], $black);
20+
imageopenpolygon($im, [10,89, 35,10, 60,89, 85,10], $red);
21+
imageopenpolygon($im, [10,49, 30,89, 50,10, 70,89, 90,10], $green);
22+
imageopenpolygon($im, [10,50, 25,10, 40,89, 55,10, 80,89, 90,50], $blue);
2323

2424
test_image_equals_file(__DIR__ . DIRECTORY_SEPARATOR . 'imageopenpolygon.png', $im);
2525
?>

ext/gd/tests/imagepolygon_aa.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $black = imagecolorallocate($im, 0, 0, 0);
1414
imagefilledrectangle($im, 0,0, 99,99, $white);
1515
imageantialias($im, true);
1616

17-
imagepolygon($im, [10,10, 49,89, 89,49], 3, $black);
17+
imagepolygon($im, [10,10, 49,89, 89,49], $black);
1818

1919
test_image_equals_file(__DIR__ . DIRECTORY_SEPARATOR . 'imagepolygon_aa.png', $im);
2020
?>

ext/gd/tests/imagepolygon_basic.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ imagepolygon($image, array (
3333
100, 200,
3434
300, 200
3535
),
36-
3,
3736
$col_poly);
3837

3938
// output the picture to a file

ext/gd/tests/libgd00100.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ $points = array(
3333
$x+$d, ($top+$bot)/2,
3434
$x, $bot
3535
);
36-
imagefilledpolygon($im, $points, 5, $yellow);
36+
imagefilledpolygon($im, $points, $yellow);
3737

3838
// left-facing M not on baseline
3939
$top = 40;
@@ -47,7 +47,7 @@ $points = array(
4747
$left, $bot,
4848
($left+$right)/2, ($top+$bot)/2
4949
);
50-
imagefilledpolygon($im, $points, 5, $purple);
50+
imagefilledpolygon($im, $points, $purple);
5151

5252
// left-facing M on baseline
5353
$top = 240;
@@ -61,7 +61,7 @@ $points = array(
6161
$left, $bot,
6262
($left+$right)/2, ($top+$bot)/2
6363
);
64-
imagefilledpolygon($im, $points, 5, $magenta);
64+
imagefilledpolygon($im, $points, $magenta);
6565

6666
// left-facing M on ceiling
6767
$top = -15;
@@ -75,23 +75,23 @@ $points = array(
7575
$left, $bot,
7676
($left+$right)/2, ($top+$bot)/2
7777
);
78-
imagefilledpolygon($im, $points, 5, $blue);
78+
imagefilledpolygon($im, $points, $blue);
7979

8080
$d = 30;
8181
$x = 150;
8282
$y = 150;
8383
$diamond = array($x-$d, $y, $x, $y+$d, $x+$d, $y, $x, $y-$d);
84-
imagefilledpolygon($im, $diamond, 4, $green);
84+
imagefilledpolygon($im, $diamond, $green);
8585

8686
$x = 180;
8787
$y = 225;
8888
$diamond = array($x-$d, $y, $x, $y+$d, $x+$d, $y, $x, $y-$d);
89-
imagefilledpolygon($im, $diamond, 4, $red);
89+
imagefilledpolygon($im, $diamond, $red);
9090

9191
$x = 225;
9292
$y = 255;
9393
$diamond = array($x-$d, $y, $x, $y+$d, $x+$d, $y, $x, $y-$d);
94-
imagefilledpolygon($im, $diamond, 4, $cyan);
94+
imagefilledpolygon($im, $diamond, $cyan);
9595

9696
// M (bridge) not touching bottom boundary
9797
$top = 100;
@@ -104,7 +104,7 @@ $points = array(
104104
$x+$d, ($top+$bot)/2,
105105
$x, $bot
106106
);
107-
imagefilledpolygon($im, $points, 5, $black);
107+
imagefilledpolygon($im, $points, $black);
108108

109109
include_once __DIR__ . '/func.inc';
110110
test_image_equals_file(__DIR__ . '/libgd00100.png', $im);

0 commit comments

Comments
 (0)