How do I set the default 'save-as' name for an image generated in PHP?
this is my code.
<?php
function generate_card($card_number='',$card_price='',$card_expire='',$image_path='',$font_path)
{
// store image on variable //
$image = imagecreatefrompng($image_path);
// some text color
$color = imagecolorallocate($image, 255, 255, 0);
// print card price //
imagettftext($image, 15, 0, 200, 40, $c开发者_开发知识库olor, $font_path, $card_price);
// print card number //
imagettftext($image, 15, 0, 15, 85, $color, $font_path, $card_number);
// print expiry date //
imagettftext($image, 12, 0, 145, 155, $color, $font_path, $card_expire);
header('Content-type:image/png');
imagepng($image);
imagedestroy($image);
}
generate_card('1234 5678 9101 1121','$200','12/13','card.png','verdana.ttf');
?>
I am generating visa gift cards, image also created and displayed on the page. But my problem is when i want to save this image in Mozilla it gives the "page_name.png" and in IE it gives me "Untitled". How can i give my own name like "visa_gift_01.png" while saving.
Read this document It explain how to modify a http header
header('Content-Disposition:attachment;filename="visa_gift_01.png"');
You can send an additional HTTP header to set the download name:
// Change the download name to visa_gift_01.png
header('Content-Disposition: attachment; filename="visa_gift_01.png"');
In order to display the image in the browser, but still provide a filename for when the user right clicks it and chooses "Save as...", here's what you want:
header('Content-Disposition: Inline; filename="'.$filename.'"');
Using Inline
instead of Attachment
is the key difference from the other answers, which will cause the image to be downloaded instead of displayed in the browser when loading the page.
What you need to add is another header:
header('Content-Disposition: attachment; filename="'.$name.'"');
Should be sufficient.
精彩评论