Page 1 of 1

Image Color Allocate Problem

Posted: Thu Jun 17, 2004 5:47 am
by kevin_javia
Hi there,

I have stored R, G, B values; size; and the text in the database.

I am able to display the text on the image, but after some rows the function imagecolorallocate returns -1 and hence failing to allocate the color according to the RGB values from database.

So when $tc = -1 in the following code, all the text after some number of rows are of same color.

What could be the reason?

Here is my code.

Code: Select all

<?php
include 'connection.php';
$cid = mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
header ("Content-type: image/png");

	$im = imagecreate(400, 300);
	$bgc = imagecolorallocate($im, 0, 0, 0);
	imagecolortransparent($im, $bgc);
	$tc = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));     
	
	
	$sql_imginf = "select * from db";
	$r_imginf = mysql_db_query($DBName,"$sql_imginf",$cid);
	$n_imginf = mysql_num_rows($r_imginf);
	
	for ($i=0;$i<$n_imginf;$i++)
	{
//here following function returns -1 after say 25 rows approx.
		$tc = imagecolorallocate($im, mysql_result($r_imginf,$i,"r"), mysql_result($r_imginf,$i,"g"), mysql_result($r_imginf,$i,"b"));     
		imagettftext($im, 15, 0, rand(0,100), rand(0,250), $tc, "fonts/1.ttf", mysql_result($r_imginf,$i,"text"));
	}			
	
	imagecolortransparent($im, $bgc);
	imagepng($im,"images/007.png");
	ImageDestroy($im);
?>
Hope someone will throw some light on this topic.

Kevin.

Posted: Thu Jun 17, 2004 7:29 am
by Wayne
How many rows do you have in the database, Im guessing around about the same number as the number times the loop runs before it breaks. Otherwise check your database values and make sure there are values in all the R G B fields.

try replacing

Code: Select all

for ($i=0;$i&lt;$n_imginf;$i++) 
   { 
//here following function returns -1 after say 25 rows approx. 
      $tc = imagecolorallocate($im, mysql_result($r_imginf,$i,"r"), mysql_result($r_imginf,$i,"g"), mysql_result($r_imginf,$i,"b"));      
      imagettftext($im, 15, 0, rand(0,100), rand(0,250), $tc, "fonts/1.ttf", mysql_result($r_imginf,$i,"text")); 
   }
... with ...

Code: Select all

while ( $row = mysql_fetch_array($r_imginf) )
   { 
//here following function returns -1 after say 25 rows approx. 
      $tc = imagecolorallocate($im, $row["r"], $row["g"], $row["b"]);      
      imagettftext($im, 15, 0, rand(0,100), rand(0,250), $tc, "fonts/1.ttf", $row["text"]); 
   }

Posted: Mon Jun 21, 2004 4:03 am
by kevin_javia
Hi Wayne,

Thanks for the reply. I am having arround 40 rows in my database. Values of R,G,B are in the range of 0 - 255.

I tried the while loop, but it does not make any difference.

Kevin.

Posted: Fri Jul 16, 2004 11:29 pm
by kevin_javia
I think no one has any solution for my problem. :(

Posted: Fri Jul 16, 2004 11:33 pm
by feyd
maybe you have too many colors active at a time? try deallocating them after you write the text..

Posted: Sat Jul 17, 2004 2:27 am
by kevin_javia
Ok feyd, I shall try that. :)

Posted: Sat Jul 17, 2004 3:19 am
by fresh
try settin a dummy value with the -1 id in the rgb table that might do the trick... ???