Image Color Allocate Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kevin_javia
Forum Newbie
Posts: 15
Joined: Mon May 24, 2004 9:46 am
Location: India
Contact:

Image Color Allocate Problem

Post 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.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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"]); 
   }
User avatar
kevin_javia
Forum Newbie
Posts: 15
Joined: Mon May 24, 2004 9:46 am
Location: India
Contact:

Post 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.
User avatar
kevin_javia
Forum Newbie
Posts: 15
Joined: Mon May 24, 2004 9:46 am
Location: India
Contact:

Post by kevin_javia »

I think no one has any solution for my problem. :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe you have too many colors active at a time? try deallocating them after you write the text..
User avatar
kevin_javia
Forum Newbie
Posts: 15
Joined: Mon May 24, 2004 9:46 am
Location: India
Contact:

Post by kevin_javia »

Ok feyd, I shall try that. :)
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

try settin a dummy value with the -1 id in the rgb table that might do the trick... ???
Post Reply