Edit report at https://bugs.php.net/bug.php?id=55005&edit=1
ID: 55005
Updated by: kalle@php.net
Reported by: lbayuk at users dot sourceforge dot net
Summary: imagepolygon, imagefilledpolygon num_points
requirement
-Status: Verified
+Status: Assigned
Type: Bug
Package: GD related
Operating System: n/a
PHP Version: 5.3.6
Assigned To: cmb
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2017-01-21 17:44:44] cmb@php.net
FTR: I've just emailed the internals list:
<http://news.php.net/php.internals/97915>.
------------------------------------------------------------------------
[2015-07-13 18:21:37] cmb@php.net
Gee!
It seems to me the cleanest solution would be to get rid of the
$num_points parameter altogether, and to only check for an even
number of array elements (and maybe for a minimum count). Of
course, that would be a major BC break, and so is out of scope
before PHP 8, at least.
Should we rely on gdImage(Filled)Polygon() to behave reasonably
for 1 and 2 points? The libgd implementation might change sometime
to require at least 3 points, and anyway, monogons and digons are
usually not recognized as polygons. So probably we shouldn't.
To avoid any BC breaks for PHP 5, I suggest to only document that
imagepolygon must have at least 3 vertices, but not to enforce
that in the code.
For PHP 7 it seems to make sense to actually check for $num_points
>= 3; see the attached patch.
------------------------------------------------------------------------
[2015-07-13 18:19:48] cmb@php.net
The following patch has been added/updated:
Patch Name: imagepolygon-numpoints
Revision: 1436811588
URL: https://bugs.php.net/patch-display.php?bug=55005&patch=imagepolygon-numpoints&revision=1436811588
------------------------------------------------------------------------
[2015-06-22 22:03:06] lbayuk at users dot sourceforge dot net
Verified still there in PHP-5.6.10
------------------------------------------------------------------------
[2011-06-06 23:26:43] lbayuk at users dot sourceforge dot net
Description:
------------
The gd extension function imagepolygon() requires at least 3 points. While this restriction is
documented for imagefilledpolygon(), it is not documented for imagepolygon(). Since both are
implemented with the same function php_imagepolygon() and contain the same check, the documentation
for imagepolygon() should be updated to match imagefilledpolygon() in requiring at least 3 points.
However, this is not just a documentation problem. The implementation does not actually check for
num_points >= 3 as the documentation for imagefilledpolygon says. It checks that the points array
size is at least 6 (3 points, 2 values each). So you can plot a 2-point or even 1-point filled or
unfilled polygon, if you use an array with 3 or more points in it but specify num_points=2 or 1.
This is very strange, not working as documented, and surely not as intended.
But there's more. As far as I can tell, libgd (bundled or stand-alone) doesn't actually
have a 3-point minimum restriction to gdImagePolygon() or gdImageFilledPolygon(), although it does
seem to be documented. See the sample code below, which plots 2-point filled and unfilled polygons
by using the above trick. It also works with num_points=1. If true, the PHP check for 3 or more
points is not needed at all.
Test script:
---------------
<?php
# Plot illegal 2-point polygon and filled polygon using over-sized array.
$g = imagecreate(300, 300);
$bgnd = imagecolorallocate($g, 255, 255, 255);
$fgnd = imagecolorallocate($g, 0, 0, 0);
imagefilledpolygon($g, array(100,10, 100,100, 180,100), 2, $fgnd);
imagepolygon($g, array(200,10, 200,100, 280,100), 2, $fgnd);
imagepng($g);
Expected result:
----------------
An error, since num_points=2 is not valid per documentation of ImageFilledPolygon.
Actual result:
--------------
A PNG image with 2 lines (2-point polygons).
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55005&edit=1