Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with GD

Status
Not open for further replies.

748323

Programmer
Dec 10, 2004
70
US
Every time I use GD, its impossible for me to use any other scripts. For example, I wrote this 'Scribble generator' when I was bored

Code:
<?

$image = imagecreate(500,500);
$back = imagecolorallocate($image, 0,0,0);

for ($x = 0; $x < rand(3,7); $x++ ) {
$arrayx = array();
$points = rand(100, 300);
for ($i = 0; $i < $points; $i++) {
$arrayx[] = rand(0, 500);
$arrayx[] = rand(0, 500);
}
$color = imagecolorallocate($image, rand(0,255) ,rand(0,255),rand(0,255));
imagepolygon($image, $arrayx, $points, $color);
}


Header("Content-type: image/png");
imagepng($image);

?>

This will create a new PNG image, and put a ton of random scribbles in there. Now, when I try to echo something before or after that script, it gives me an error. For example:

Code:
<?
echo "sa";

$image = imagecreate(500,500);
$back = imagecolorallocate($image, 0,0,0);
Header("Content-type: image/png");
imagepng($image);

?>

That script will give me an error saying that the Headers cannot be modified. Help? Thanks!
 
Think about what you are doing here.

You're creating an image an displaying it. One image per invocation of your script. Just invoke your script via the <img> tag in HTML then you can do whatever you like before and after.

Code:
<?
echo 'something';
echo '<img src=yourscript.php>';
echo 'something else';
?>

Ken
 
Just a general tip:
Header() and session_start() are two functions that *must* be called *before* anything is parsed!

You also can not echo anything in the image-generating script, as that will not work!

When you call the image-making-script, it will make an image for the client (web-browser) and show that image.
If you however try to echo or do anything else, before the header, it will not work, as then the headers are already sent. (as you then make text, not image).

btw. use quotes in your html tags:
<img src="yourscript.php" alt="foo" />

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top