如何更改“另存为..."中显示的文件名从.php到.png的对话框 [英] How to change the filename displayed in the "Save as..." dialog from .php to .png

查看:81
本文介绍了如何更改“另存为..."中显示的文件名从.php到.png的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从stackoverflow中选取的一个简单的PHP脚本会生成具有透明背景的PNG,在其上写入一些文本,然后将其直接输出到客户端浏览器:

A simple PHP script that I picked up from stackoverflow generates a PNG with a transparent background, writes some text on it then directly outputs it to the client browser:

$font = 25;
$string = 'My Text';
$im = @imagecreatetruecolor(300, 300);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, 30, $red, "fonts/tt0588m_.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

范围是获得一种简单的服务,该服务根据通过URL传递给它的参数(例如Google图表)来馈送图像(例如

The scope is to obtain a simple service that feeds an image based on the parameters passed to it via URL, such as Google Charts (e.g. this QR code image).

到目前为止,一切都很好,除了如果我单击上面代码生成的图像并想要保存它,浏览器不会将其识别为PNG图像,而是一个 PHP脚本(另存为类型选择器仅具有此选项),与Google图表示例相反,该示例将资源明确标识为PNG文件.

So far, so good, except that if I click on the image generated with the code above and want to save it, the browser doesn't recognize it as being a PNG image, but a PHP script (Save as type selector has this option only), as oposed to the Google Charts example, where the resource is clearly identified as a PNG file.

如何通过浏览器正确识别资源?

How do I achieve this correct resource identification by the browser?

推荐答案

浏览器将使用URL中的文件名作为另存为..."对话框中的默认值.当然,您可以键入另一个名称,也可以使用建议的名称(text.php)保存文件,然后再重命名.

Browser will use the filename from the URL as the default value in the "Save as..." dialog. You can type another name of course or save the file using the suggested name (text.php) and rename it afterwards.

您可以使用 Content-disposition 标头向浏览器建议"文件名.这是一个示例:

You can use the Content-disposition header to "suggest" a filename to the browser. Here is an example:

header("Content-type: image/png");
header("Content-disposition: inline; filename=mytext.png");

  • inline建议浏览器应尝试显示图像.将其更改为attachment以建议浏览器应显示另存为..."或类似对话框.
  • filename=应包含您选择的文件名.
    • inline suggests that browser should attempt to display the image. Change it to attachment to suggest that the browser should display the "Save as..." or similar dialog.
    • filename= should contain the filename of your choice.
    • 这篇关于如何更改“另存为..."中显示的文件名从.php到.png的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆