0

So I have written a GD image 'modifier' we'll call it, and using the imagejpeg function, I output it to the browser. I'm curious if there is perhaps header information that I can pass that will instruct the browser to download the file with a name other than that of the PHP file that is handing the data back.

The scenario is that my PHP file, 'generate.php' for now, hands an image header out and outputs the image, but if I go to save the image, the default filename is 'generate.php'. I'd like to hand back a dynamically generated filename instead.

If code is a must here for some reason; I'll be glad to post it - but I feel it's not necessary in this case.

DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41

2 Answers2

3

Send a Content-Disposition header.

To display the image:

header("Content-Disposition: Inline; filename=$filename");

To download the image:

header("Content-Disposition: Attachment; filename=$filename");
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Nailed it. Thank you. Just for anyone else that stumbles across this; the 'inline' disposition forces naming of the file; the 'attachment' disposition forces naming -and- begins a download. Cheers @AbraCadaver – DigitalJedi805 Jan 21 '15 at 21:40
0

I think you can rewrite the url:

URL rewriting with PHP

You can add a simple rule like this to the .htaccess file:

RewriteEngine on RewriteRule ^/images/(.*)$ /generate.php?imagename=$1

When you are calling the image you are going to use <img src="/images/image.png" /> but the server instead of search for that url is going to execute your php script because of your rule.

Community
  • 1
  • 1
Daniel
  • 1