View Full Version : GD output to file?

01-20-2003, 10:05 PM

I know how to use GD to create images on the fly (such as nowplaying gfx ;)). My question is: is it posible to have the image outputted to a file stored on the server (eg /imgs/on-the-fly/NUM.png where NUM is a number) rather than inserted into the page using <img src="mygdscript.php">?

I would use this to create headers in a certain font. A PHP script checks if the graphic already exists (fairly simple I assume if a common naming system is employed) and if not commissions the GD script to output the created image to a file, so in future it is already there - no need to make it for every visitor.

Is this possible and if so, how?

Dylan Leblanc
01-21-2003, 05:34 AM
I have not worked with image in PHP, but I have seen how they work. You might be able to use output buffering to capture the output and then save it as a file.

01-21-2003, 11:13 AM
depending upon which format you are parsing the image as, the create as file option resides within using extra parameters in the ImageFormat() call.


ImagePNG($image_file); => output to browser
ImagePNG($image_file,'images/image.png'); => output to file as named.

With jpegs there is a third parameter for compression --

ImageJPG($image_file,'',85); => to browser at 85% quality.

Have a look on php.net for the exact format you are outputting in.

01-21-2003, 06:02 PM
Thanks a lot, will experiment with that later tonight. First got a little HDD to put in my bro's PC. Wish me luck!

01-21-2003, 11:59 PM
Allright I've got it outputing to a file. However, I am worried that the folder to which it outputs MUST be 777 (allow all read,write,execute) as this leaves this folder vulnerable to writing or deleting from outside. Is this preventable?

I know also have a script checking whether a image is already created or not. My problem now is if it already exists passing it back to the original <img src="checkpage.php?text=hello world">. I'm getting red X's. Here's my code so far:


$imgstore = "imgstore/";

if (!$text) {
die(); // die if no text specified, duh

if (!$maxwidth) {
$maxwidth="400"; // default width if none specified.

//check if header already exists.

$filename = $imgstore.str_replace(" ","_",$text).".png";

if (file_exists($filename)) {
//exists so pass file back
$imgpointer = fopen($filename, 'r');
$imgcode = fread($imgpointer,filesize($imgpointer));
return $imgcode;
else {
echo "bumbum"; // don't exist buddy


Don't worry about my immature error messages or comments -- a guy's gotta have fun coding.

Am I approaching this the right way? If not, how should I handle it?

01-22-2003, 08:30 PM
CODAAh [createOnDemandAndArchive] is done! Yay!

Basically, it will be used on my site. It will use a destinctive font unlikely to reside on the users system. As a result article headers will have to be created. I'm too lazy to make them. So I wrote this script to do it for me. (Obviously it could be adapted to create different colour / style text imgs)

If you wanna try it out, the URL is: http://skinme.net/misc/createOnDemand/test.php?text=*PUT A SMALLISH (400px worth) TEXT STRING HERE*. I will be moving it sooner or later, though. (BTW, the white is a transparent dark blueish colour used on my site)

Thanks for all your help. /me learnt a lot with this script. Most importantly, read what you've written when you get errors before you got changing ALL the code :D

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum