PNG image help please

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
dnathe4th
Forum Newbie
Posts: 16
Joined: Fri Jun 17, 2005 2:01 pm

PNG image help please

Post by dnathe4th »

I'm trying to generate a picture with dots on it, the coordinates of the dots provided by a database table with the format of ID, X-co, Y-co

Code: Select all

<?PHP
$im = ImageCreateFromPNG("1.png") or die("No go on the PNG");

$size = '12';
$angle = '0';
$font = "Bamboo.ttf";
$textstr = "*";


mysql_connect("www.freesql.org","dnathe4th","THISISNOTMYPASSWORD") or die("Unable to connect");
@mysql_select_db("DNA_Fury_members") or die( "Unable to select database");
$query = "SELECT * FROM click_research";
$result = mysql_query($query) or die("Unable to submit query");
$num = mysql_numrows($result);
mysql_close();
$i = 0;
while($i < $num)
{
	$color = ImageColorAllocate($im, rand(0, 100), rand(0, 100), rand(0, 100)) or die("Unable to allocate color");
	$x = mysql_result($result, $i, "x");
	$y = mysql_result($result, $i, "y");
	ImageTTFText($im, $size, $angle, $x, $y, $color, $font, $textstr) or die("Could not do whatever this does");
	$i++;
};

header("Content-Type: image/x-png");
echo ImagePNG($im);
?>
Ignore all the random Die statements, those were just my troubleshooting. Most of the code was copied and pasted from other places, then modified, which explains why it doesnt work. Can someone please help me here?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what is the error you are getting?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Don't echo the call to ImagePNG($im). Just use ImagePNG($im);.
dnathe4th
Forum Newbie
Posts: 16
Joined: Fri Jun 17, 2005 2:01 pm

Post by dnathe4th »

ya i took out the echo after I posted, but it still doesn't work. There is no error, the page just doesn't come up when I view it in a browser. Is there any way I can run it to get an error? That would help with troubleshooting.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

dnathe4th wrote:Is there any way I can run it to get an error? That would help with troubleshooting.
Comment out lines 26 and 27.
dnathe4th
Forum Newbie
Posts: 16
Joined: Fri Jun 17, 2005 2:01 pm

Post by dnathe4th »

nah, even after commenting out lines 26 and 27 the page is still just blank when i try to access it. :(
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

dnathe4th wrote:nah, even after commenting out lines 26 and 27 the page is still just blank when i try to access it. :(
Line 14: mysql_num_rows() is missing an _
dnathe4th
Forum Newbie
Posts: 16
Joined: Fri Jun 17, 2005 2:01 pm

Post by dnathe4th »

i put the _ in the num_rows but its still not showing anything. I don't know what else to try
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You're closing your mysql connection and then trying to use the result in the loop.

Try this:

Code: Select all

<?php

	$im = ImageCreateTrueColor(200,200);

	$size = '12';
	$angle = '0';
	$font = "Bamboo.ttf";
	$textstr = "*";

	mysql_connect("www.freesql.org","dnathe4th","THISISNOTMYPASSWORD");
	@mysql_select_db("DNA_Fury_members");
	$result = mysql_query("SELECT * FROM click_research");

	while($record = mysql_fetch_object($result))
	{
		$color = ImageColorAllocate($im, rand(0, 100), rand(0, 100), rand(0, 100));
		$x = $record->x;
		$y = $record->y;
		ImageTTFText($im, $size, $angle, $x, $y, $color, $font, $textstr);
	};

	header("Content-Type: image/png");
	ImagePNG($im);

?>
Note that I've made it create a new image instead of opening one.. that was just me checking it'd work.
dnathe4th
Forum Newbie
Posts: 16
Joined: Fri Jun 17, 2005 2:01 pm

Post by dnathe4th »

onion did that work for you? its still not working for me. Still no image if i open it directly, and if i open a page with <IMG src=whatever.php> i just get a broken image. I used the exact code u pasted there onion, save, i changed my password.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

My version is below, I had to tweak a few things to get rid of the database stuff. Works fine. I take it you definitely have GD installed with the FreeType library?

Code: Select all

<?php

	$im = ImageCreate(200,200);
	$color = ImageColorAllocate($im, 255, 255, 255);

	$size = '12';
	$angle = '0';
	$font = "YIKES!__.TTF";
	$textstr = "*";
	$num = 10;

//	mysql_connect("www.freesql.org","dnathe4th","THISISNOTMYPASSWORD") or die("Unable to connect");
//	@mysql_select_db("DNA_Fury_members") or die( "Unable to select database");
//	$result = mysql_query("SELECT * FROM click_research");

	//while($record = mysql_fetch_object($result))
	//{
		$color = ImageColorAllocate($im, rand(0, 100), rand(0, 100), rand(0, 100));
		//$x = $record->x;
		//$y = $record->y;
		$x = rand(0,200);
		$y = rand(0,200);
		ImageTTFText($im, $size, $angle, $x, $y, $color, $font, $textstr);
	//};

	header("Content-Type: image/png");
	ImagePNG($im);

?>
Post Reply