Jason
Jason

Reputation: 708

'Save As' image with src from PHP

I'm currently displaying images and hiding the src code by having a php file output the image. But when I right click on the image displayed and go down to 'Save As' it prompts me to download the php file not the actual image (obviously because it points to that src).

What can I do to download the actual image instead of displayImage.php?

Upvotes: 3

Views: 1654

Answers (3)

MrCode
MrCode

Reputation: 64526

  1. Send the correct content type in the image generator script:

    header('Content-type: image/jpg');

  2. If you want to have the .jpg extension when a PHP script is outputting an image, you'll need to do a htaccess or httpd.conf rewrite, where you can rewrite a .jpg request, to your php image generator script.

See mod_rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88647

It doesn't prompt you to download the PHP file, it simply uses that as the file name, because that is the file name from which it got the image data. If you manually input a valid image file name and try to open what you saved, it should still be a valid image.

You may also be able to give it a sensible name by including the file name in a Content-Disposition: header from your PHP file, e.g.

 $filename = 'image.jpg';
 header('Content-Disposition: inline; filename="'.$filename.'"');
 // Don't forget the Content-Type as well...
 // Output image here

...however this relies on the browser handling this sensibly, which not all of them do :-(

Upvotes: 6

PiTheNumber
PiTheNumber

Reputation: 23542

You can send a filename in the header.

header("Content-Type: image/png");
header('Content-Disposition: inline; filename="some.png"');

Upvotes: 3

Related Questions