Skip to content

Commit 72b73e2

Browse files
committed
Fix #53156: imagerectangle problem with point ordering
Contrary to imagefilledrectangle(), imagerectangle() has the documented limitation that the given points have to be the upper left and the lower right corner, respectively. However, libgd already caters to upper right / lower left pairs, and not catering to the other two combinations seems to be an oversight.
1 parent 90de2ae commit 72b73e2

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

ext/gd/libgd/gd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,9 @@ void gdImageRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, int color)
20442044
t=y1;
20452045
y1 = y2;
20462046
y2 = t;
2047-
2047+
}
2048+
2049+
if (x2 < x1) {
20482050
t = x1;
20492051
x1 = x2;
20502052
x2 = t;

ext/gd/tests/bug53156.phpt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--TEST--
2+
Bug #53156 (imagerectangle problem with point ordering)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
function draw_and_check_pixel($x, $y)
10+
{
11+
global $img, $black, $red;
12+
13+
echo (imagecolorat($img, $x, $y) === $black) ? '+' : '-';
14+
imagesetpixel($img, $x, $y, $red);
15+
}
16+
17+
function draw_and_check_rectangle($x1, $y1, $x2, $y2)
18+
{
19+
global $img, $black;
20+
21+
echo 'Rectangle: ';
22+
imagerectangle($img, $x1, $y1, $x2, $y2, $black);
23+
draw_and_check_pixel(($x1 + $x2) / 2, $y1);
24+
draw_and_check_pixel($x1, ($y1 + $y2) / 2);
25+
draw_and_check_pixel(($x1 + $x2) / 2, $y2);
26+
draw_and_check_pixel($x2, ($y1 + $y2) / 2);
27+
echo PHP_EOL;
28+
}
29+
30+
$img = imagecreate(110, 210);
31+
$bgnd = imagecolorallocate($img, 255, 255, 255);
32+
$black = imagecolorallocate($img, 0, 0, 0);
33+
$red = imagecolorallocate($img, 255, 0, 0);
34+
35+
draw_and_check_rectangle(10, 10, 50, 50);
36+
draw_and_check_rectangle(50, 60, 10, 100);
37+
draw_and_check_rectangle(50, 150, 10, 110);
38+
draw_and_check_rectangle(10, 200, 50, 160);
39+
imagesetthickness($img, 4);
40+
draw_and_check_rectangle(60, 10, 100, 50);
41+
draw_and_check_rectangle(100, 60, 60, 100);
42+
draw_and_check_rectangle(100, 150, 60, 110);
43+
draw_and_check_rectangle(60, 200, 100, 160);
44+
45+
imagepng($img, __DIR__ . '/bug53156.png');
46+
?>
47+
--CLEAN--
48+
<?php
49+
@unlink(__DIR__ . '/bug53156.png');
50+
?>
51+
--EXPECT--
52+
Rectangle: ++++
53+
Rectangle: ++++
54+
Rectangle: ++++
55+
Rectangle: ++++
56+
Rectangle: ++++
57+
Rectangle: ++++
58+
Rectangle: ++++
59+
Rectangle: ++++

0 commit comments

Comments
 (0)