php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53156 imagerectangle problem with point ordering
Submitted: 2010-10-25 21:50 UTC Modified: 2015-07-20 00:38 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: lbayuk at users dot sourceforge dot net Assigned: pajoye
Status: Closed Package: GD related
PHP Version: 5.6.11 OS: Linux
Private report: No CVE-ID:
 [2010-10-25 21:50 UTC] lbayuk at users dot sourceforge dot net
Description:
------------
The documentation says imagerectangle() requires the upper left point first, then the lower right, whereas imagefilledrectangle() accepts the points in any order. In fact, imagerectangle() does work with all 4 possible orderings of the points, but only when drawing with unit thickness.  When drawing rectangles with thickness > 1, only 2 of the 4 cases work.

Before marking this as "working as documented" (which it is), please consider that there seems to be incorrect logic in gdImageRectangle() when handling the points.  It tests for y2<y1 and if so it swaps both X and Y. It should test and swap X and Y independently, same as gdImageFilledRectangle does. So it seems to already be trying to avoid this requirement for point order, but it doesn't quite work in all cases.

Test script:
---------------
<?php
$img = imagecreate(400, 800);
$bgnd  = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
imagesetthickness($img, 4);
imagestring($img, 4, 20, 140, 'Order: upper left, lower right', $black);
imagerectangle($img, 20, 20, 120, 120, $black);
imagefilledrectangle($img, 15, 15, 25, 25, $green); // Mark start point
imagestring($img, 4, 20, 340, 'Order: lower left, upper right', $black);
imagerectangle($img, 20, 320, 120, 220, $black);
imagefilledrectangle($img, 15, 315, 25, 325, $green); // Mark start point
imagestring($img, 4, 20, 540, 'Order: Upper right, lower left', $black);
imagerectangle($img, 120, 420, 20, 520, $black);
imagefilledrectangle($img, 115, 415, 125, 425, $green); // Mark start point
imagestring($img, 4, 20, 740, 'Order: Lower right, upper left', $black);
imagerectangle($img, 120, 720, 20, 620, $black);
imagefilledrectangle($img, 115, 715, 125, 725, $green); // Mark start point
imagepng($img);

Expected result:
----------------
4 rectangles, drawn with thick lines.

Actual result:
--------------
2 rectangles, and two sets of parallel lines.

Patches

gd-rect-order-bug (last revision 2010-10-25 19:53 UTC) by ljb9832 at pobox dot com)

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-25 16:28 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: pajoye
 [2013-06-25 16:28 UTC] felipe@php.net
What do you think about, Pierre?
 [2015-06-22 21:41 UTC] lbayuk at users dot sourceforge dot net
-: ljb9832 at pobox dot com +: lbayuk at users dot sourceforge dot net
 [2015-06-22 21:41 UTC] lbayuk at users dot sourceforge dot net
Verified still there in PHP-5.6.10
 [2015-07-20 00:38 UTC] cmb@php.net
-Status: Assigned +Status: Verified -PHP Version: 5.3.3 +PHP Version: 5.6.11
 [2015-07-20 00:38 UTC] cmb@php.net
I can confirm the behavior.

IMHO this is more of a bug than a documentation issue. Therefore
I've submitted a PR with lbayuk's patch.
 [2015-07-20 23:26 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=72b73e24a00b9872214fbcc256ee38431d9c9d47
Log: Fix #53156: imagerectangle problem with point ordering
 [2015-07-20 23:26 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2015-07-21 14:20 UTC] ab@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=72b73e24a00b9872214fbcc256ee38431d9c9d47
Log: Fix #53156: imagerectangle problem with point ordering
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Feb 26 21:01:35 2017 UTC