How to set the default value for "save-as" 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, $color, $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 create gift cards for visas, an image is also created and displayed on the page. But my problem is when I want to save this image in Mozilla, it gives "page_name.png", and in IE it gives me "Untitled". How can I give my name, for example, "visa_gift_01.png" when saving.

+4
source share
5 answers

Read this document . It explains how to change the http header.

header('Content-Disposition:attachment;filename="visa_gift_01.png"');
+5
source

HTTP header, :

// Change the download name to visa_gift_01.png
header('Content-Disposition: attachment; filename="visa_gift_01.png"');
+4

, :

header('Content-Disposition: attachment; filename="'.$name.'"');

.

0
header('Content-Disposition:filename="visa_gift_01.png"');
0

To display the image in the browser, but specify the file name when the user right-clicks on it and selects "Save as ...", this is what you want:

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

Using Inlineinstead Attachmentis a key difference from other answers, as a result of which the image is loaded, and not displayed in the browser when the page loads.

0
source

Source: https://habr.com/ru/post/1775374/


All Articles