php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46466 Imagecolorallocate,imagettftext,rand combined bug
Submitted: 2008-11-02 21:59 UTC Modified: 2008-11-04 11:26 UTC
From: laszlo dot janszky at gmail dot com Assigned: pajoye (profile)
Status: Not a bug Package: GD related
PHP Version: 5.2.6 OS: windows xp
Private report: No CVE-ID: None
 [2008-11-02 21:59 UTC] laszlo dot janszky at gmail dot com
Description:
------------
I'm calling imagecolorallocate and imagettftext functions with random parameters in a loop, but after some call the imagecolorallocate function returns FALSE. I checked the random parameters, but nothing strange about them.
I think the bug is associated with the rand function, because the more I call the rand function, the earlier the bug comes in loop. If I delete the rand from the 5 times I call, and replace it with constant number, the bug comes later in cycle. With this settings it's about character 'w', but not on a constant place, somtimes 'x' or 'y'.


Reproduce code:
---------------
		$text=str_split('0123456789abcdefghijklmnopqrstvwxyz',1);
		$image=imagecreate(600,50);
		$bgcolor=imagecolorallocate($image,0,200,130);
		foreach($text as $char)
		{
			$r=rand(0,30);
			$b=255-rand(0,30);
			$g=138+rand(-15,15);
			
			$a=rand(0,10);
			$l=15+$len*15;
			$t=35+rand(-5,5);
			
			$s=20+rand(-2,2);
			
			imagettftext($image,$s,$a,$l,$t,imagecolorallocate($image,$r,$b,$g),'xfont.ttf',$char);
			$len++;
		}
		header("Expires: Mon,26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: ".gmdate("D,d M Y H:i:S")." GMT");
		header("Cache-Control: no-store,no-cache,must-revalidate");
		header("Cache-Control: post-check=0,pre-check=0",false);
		header("Pragma: no-cache");
		header("Content-type: image/png");
		imagepng($image);
		imagedestroy($image);

Expected result:
----------------
I expect to get an image with characters '0123456789abcdefghijklmnopqrstvwxyz' one after the other with different(random) angles and colour.

Actual result:
--------------
I got an image with characters '0123456789abcdefghijklmnopqrstvw', so not the complete character list. And didn't get any error messages or notices with error_reporting(2047).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-02 22:23 UTC] pajoye@php.net
what about checking the colors you get? Like the random values may be duplicated or ends using the bg color.

There is no bug in either the ttf or the color function. Try to predefine the colors to verify it.
 [2008-11-02 23:09 UTC] laszlo dot janszky at gmail dot com
http://hu.php.net/imagecolorallocate
I had read the documentation before I sent the report. I wrote an if in my code like this:

$c=imagecolorallocate($image,$r,$b,$g);
imagettftext($image,$s,$a,$l,$t,$c,'xfont.ttf',$char);

$c was false, but the $r,$b,$g parameters were valid, and they weren's the same as the background.

I checked it with another ttf file, but nothing changed. 

I'll check it tomorrow without rand manually, but I'm afraid it's caused by that function.
 [2008-11-02 23:12 UTC] laszlo dot janszky at gmail dot com
Nah I wrote wrong code in my previous comment, here is the right one:

$c=imagecolorallocate($image,$r,$b,$g);
if (!$c)
{
  echo($r.','.$b.','.$g.'<br>');
}
imagettftext($image,$s,$a,$l,$t,$c,'xfont.ttf',$char);
 [2008-11-03 08:34 UTC] laszlo dot janszky at gmail dot com
Now I checked it manually, i saved all the settings in a table, and loaded it. It's generating the bug without rand() too.

Here is the table:

$c     $char $r    $b      $g      $a    $s     $l      $t
1;     0;    0;    237;    150;    6;    20;    15;     31
9;     1;    5;    250;    142;    3;    20;    30;     33
17;    2;    23;   237;    130;    9;    19;    45;     33
25;    3;    21;   255;    147;    10;   18;    60;     37
33;    4;    28;   240;    150;    8;    19;    75;     30
41;    5;    10;   243;    124;    5;    21;    90;     36
49;    6;    8;    240;    149;    7;    18;    105;    35
57;    7;    18;   247;    128;    1;    21;    120;    30
65;    8;    2;    249;    142;    5;    22;    135;    36
72;    9;    17;   225;    138;    3;    22;    150;    34
80;    a;    5;    230;    131;    6;    19;    165;    35
88;    b;    22;   240;    145;    4;    18;    180;    39
96;    c;    24;   226;    132;    9;    20;    195;    30
104;   d;    3;    249;    129;    10;   21;    210;    40
112;   e;    22;   230;    146;    8;    18;    225;    34
120;   f;    9;    255;    144;    1;    20;    240;    36
127;   g;    8;    255;    129;    3;    18;    255;    36
135;   h;    8;    246;    136;    6;    19;    270;    34
143;   i;    6;    233;    142;    7;    20;    285;    37
151;   j;    18;   225;    131;    5;    20;    300;    33
159;   k;    24;   236;    140;    10;   19;    315;    36
167;   l;    7;    240;    137;    6;    22;    330;    30
175;   m;    25;   229;    124;    4;    18;    345;    35
183;   n;    25;   233;    146;    9;    18;    360;    35
211;   o;    17;   254;    142;    0;    18;    375;    32
218;   p;    10;   254;    138;    1;    21;    390;    32
224;   q;    25;   243;    132;    2;    22;    405;    30
232;   r;    8;    242;    135;    4;    20;    420;    32
239;   s;    26;   239;    143;    10;   22;    435;    37
245;   t;    22;   248;    147;    4;    18;    450;    30
252;   v;    14;   237;    143;    5;    20;    465;    36
FALSE; w;    6;    243;    136;    3;    22;    480;    38
FALSE; x;    28;   246;    150;    9;    22;    495;    34
FALSE; y;    22;   247;    142;    8;    18;    510;    39
FALSE; z;    0;    232;    133;    3;    21;    525;    34


$c is the returnValue of colorallocate function, $char is the current character, $r,$b,$g -> red,green,blue, $a -> angle, $s -> font size, $l,$t -> font left and top.

As you see, the returnValue of the colorallocate function behaves very strange.
 [2008-11-03 08:43 UTC] pajoye@php.net
Please provide a small script to reproduce it. Do not use anything but the image function and the PHP core functions (no rand, db, input data, etc.).

And keep in mind that palette images can have maximum 256 colors (255 + bgd).
 [2008-11-04 10:20 UTC] laszlo dot janszky at gmail dot com
OK! It looks like that I exceeded the 256 colors limit. Thanks to everbody.
 [2008-11-04 11:26 UTC] pajoye@php.net
No bug > bogus
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 19:01:30 2024 UTC