PHP :: Bug #53154 :: Zero-height rectangle has whiskers

Bug #53154 Zero-height rectangle has whiskers
Submitted: 2010-10-25 21:31 UTC Modified: 2015-07-12 23:53 UTC
From: lbayuk at users dot sourceforge dot net Assigned: cmb (profile)
Status: Closed Package: GD related
PHP Version: 5.6.10 OS: *
Private report: No CVE-ID: None

 [2010-10-25 21:31 UTC] lbayuk at users dot sourceforge dot net

Description:
------------
Using imagerectangle() to draw a rectangle that happens to have zero height results in a horizontal line with small 'whiskers' at the ends. The whiskers extend up and down 1 pixel from the line. This looks weird. This only happens with unit line thickness. It does not happen with zero width rectangles, nor with imagefilledrectangle().

Note: Using PHP-bundled GD library.

Test script:
---------------
<?php
$img = imagecreate(400, 200);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, 4, 5, 5, "RECTANGLES", $black);
imagestring($img, 4, 150, 5, "Filled", $black);
imagestring($img, 4, 300, 5, "Unfilled", $black);
imagestring($img, 4, 5, 75, "Height=0", $black);
imagestring($img, 4, 5, 145, "Width=0", $black);
imagefilledrectangle($img, 150, 95, 200, 95, $black);
imagerectangle($img, 300, 95, 350, 95, $black);
imagefilledrectangle($img, 175, 125, 175, 175, $black);
imagerectangle($img, 325, 125, 325, 175, $black);
imageellipse($img, 325, 95, 90, 50, $red); // Circle the bad one
imagepng($img);

Expected result:
----------------
In all 4 cases, a single line should be drawn.

Actual result:
--------------
The upper right case (zero height, unfilled rectangle) has whiskers at the ends.

Patches

zero-rectangle (last revision 2015-06-29 11:34 UTC by cmb@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

 [2015-06-22 21:36 UTC] lbayuk at users dot sourceforge dot net

-: ljb9832 at pobox dot com +: lbayuk at users dot sourceforge dot net

 [2015-06-22 21:36 UTC] lbayuk at users dot sourceforge dot net

Verified still there in PHP-5.6.10

 [2015-06-28 20:15 UTC] cmb@php.net

-Status: Open +Status: Verified -Operating System: Linux +Operating System: * -PHP Version: 5.3.3 +PHP Version: 5.6.10 -Assigned To: +Assigned To: cmb

 [2015-06-28 22:28 UTC] lbayuk at users dot sourceforge dot net

(Sorry for the 'still there' comment... I needed to change my email on the report, and it doesn't seem to work unless a new comment is added. (Should I open a bug report on the bug system?)) 

By "two assignments", do you mean y1v=y1h+1 and y2v=y2h-1 ? Or in the libgd code in your second link, the in-line equivalents in the 3rd and 4th calls to gdImageLine() ?

These seem to be there to prevent the corner pixels from being drawn twice.  That is, the vertical line segments that make up the rectangle are 2 pixels shorter than the sides of the rectangle. When the rectangle height is > 0 this prevents the corner pixels from being drawn twice, once as part of the horizontal segments and once as part of the vertical segments.

But in the edge case of rectangle height = 0, this code causes the "whiskers" on the left and right ends.

Why would you want to prevent the corner pixels from being drawn twice?  The only reason I can think of where it matters is when alpha blending is on (as it is by default with truecolor images). In this case, drawing over the same pixel with the same color more than once will darken it each time.  This will be true when the color is partially transparent (alpha > 0).

The effect is subtle, but can be seen with a screen magnifier. 

That's my guess, anyway.

 [2015-06-29 11:35 UTC] cmb@php.net

-Status: Verified +Status: Analyzed

 [2015-06-29 11:35 UTC] cmb@php.net

No need to apologize. IMHO it is fine to remind that a bug is
still there in recent versions after nearly 5 years.

Anyhow, thanks for your analysis; the behavior to draw the corner
pixels only once makes sense, and should be kept. In the same
manner, there probably should be separate cases for rectangles
with zero widht or height (drawing a single line in this case
would be sufficient), as done in the "zero-rectangle" patch.

 [2015-06-29 20:15 UTC] lbayuk at users dot sourceforge dot net

I rebuilt the PHP GD extension with your patch, for PHP-5.6.10. I can confirm it fixed the bug and I saw no regressions.

 [2015-07-12 23:51 UTC] cmb@php.net

-Status: Analyzed +Status: Closed

 [2015-07-12 23:53 UTC] cmb@php.net

Thanks for the verification, lbayuk. :)