[thelist] Filename on php/gd-generated images

Kasimir K evolt at kasimir-k.fi
Tue May 24 12:59:22 CDT 2005

Maximillian Schwanekamp scribeva in 2005-05-24 17:21:
> I've a php script (check.php) that generates an image according to query 
> string params using GD, and outputs the image directly to browser with 
> imagejpeg().  Works fine, but I'm wondering if someone can tell me how 
> to set the filename to something other than the calling script when 
> outputting straight to browser.  So if the user saves the image to their 
> local machine, the filename will be "imagename.jpg" (or similar) instead 
> of "check.php".  I'm trying to avoid saving the image to filesystem...

<quote from="http://php.net/imagejpeg">
bool imagejpeg ( resource image [, string filename [, int quality]] )

imagejpeg() creates the JPEG file in filename from the image image. The 
image argument is the return from the imagecreate() function.

The filename argument is optional, and if left off, the raw image stream 
will be output directly. To skip the filename argument in order to 
provide a quality argument just use an empty string (''). By sending an 
image/jpeg content-type using header(), you can create a PHP script that 
outputs JPEG images directly.

I use often this kind of script:

header("Content-type: image/jpg");
header("Content-Disposition: "
    . ($inline ? 'inline':'attachment')
    . "; filename=$filename);

Using the variable $inline I can choose whether the image is displayed 
on the browser or prompted for download.


More information about the thelist mailing list