PDA

View Full Version : images in php


jonathaw
01-04-2005, 11:11 PM
I want to use some of the php image functions, but it's not working. I used this example straight from the php online documentation, but it just returns code, which is not ASCII-friendly.

http://test.jonathanadairwilson.com/test.php

You can see what I'm talking about here. Anyone have any ideas? I've been spending waaaay to much time on this and I can't think straight anymore tonight. Thanks!!!

fturtle
01-04-2005, 11:50 PM
It's missing the first line, the header() function, which in this case sets the content type and defines how it's handled by the browser. The header() function always has to be placed before a script sends anything to the browser..which shouldn't be something to worry about with image scripts, since the purpose is to generate an image.

The idea is to have a separate php file for the images, which you reference as an image:
<img src="image_php_file.php">The reference can also use url variables, which can give you dynamic images:
<img src="image_php_file.php?var=value">Here's the example from the php site. Yours is the same, but without the header function:

<?php
header ("Content-type: image/png");
$im = @imagecreatetruecolor(50, 100)
or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

jonathaw
01-05-2005, 03:02 PM
So I fixed it, it totally works now! Thanks!
Now I can learn fancier things...