如何设置默认的“图像另存为” PHP 生成的图像的名称?

发布于 2024-11-17 16:05:07 字数 286 浏览 7 评论 0原文

我的网站中有一个页面,显示我的 PHP 生成的一些图像。当我右键单击图像并单击“将图像保存为”时,我得到用于生成图像的 php 文件的名称作为默认名称。

例如,这是图像的 html:,

 <img src="picture_generator.php?image_id=5&extension=.png">

我得到的名称是: picture_generator.php.png

有没有办法将此名称设置为默认名称?

提前致谢

I have a page in my site, displaying some images that are produced my PHP. When I right click on an image and click Save Image As I get as default name the name of the php file used for generating the image.

This is for example the html for the image :

 <img src="picture_generator.php?image_id=5&extension=.png">

and the name I get is:
picture_generator.php.png

Is there a way to set this name to a default one?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风铃鹿 2024-11-24 16:05:07

您可以在 Content-Disposition HTTP 标头中提供它:

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

但是,某些浏览器(即 Internet Explorer)可能会忽略此标头。最安全的解决方案是伪造 URL,让浏览器相信它正在下载 /images/5/foo.png 这样的静态文件,而实际路径背后是场景是/picture_generator.php?image_id=5&extension=.png。这可以通过一些 Web 服务器模块来完成,例如 Apache 的 mod_rewrite

You can provide it in the Content-Disposition HTTP header:

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

However, some browsers (namely Internet Explorer) are likely to ignore this header. The most bullet-proof solution is to forge the URL and make the browser believe it's downloading a static file like /images/5/foo.png while the actual path behind the scenes is /picture_generator.php?image_id=5&extension=.png. This can be accomplished by some web server modules like Apache's mod_rewrite.

乱世争霸 2024-11-24 16:05:07

您可以尝试使用 HTTP 标头设置文件名,但并非所有浏览器都遵循这一点。

最简单的技巧是扩展 URL,以便最后一部分包含所需的文件名:

<img src="picture_generator.php/desiredfilename.jpg?image_id=5&extension=.png&name=desiredfilename.jpg">

注意,我还在查询字符串的末尾添加了文件名(name 并不重要),如下所示有些浏览器使用该部分。

根据您的服务器配置,这将立即工作,无需任何特殊配置(没有 mod_rewrite 或类似的东西)。您只需将“/foo”附加到您网站上的任何 PHP-URL 即可检查它是否在您的服务器上运行。如果您看到 PHP 的输出,则一切都很好。如果您看到 404 错误,则说明您的服务器配置无法处理此类 URL。

You can try to set the file name using the HTTP headers but not all browsers respect that.

The simplest trick is to extend the URL so that the last part contains the desired file name:

<img src="picture_generator.php/desiredfilename.jpg?image_id=5&extension=.png&name=desiredfilename.jpg">

Note I also added the file name at the end of the query string (the name doesn't really matter) as some browsers use that part.

Depending on your server configuration this will immediately work without any special configuration (no mod_rewrite or anything like that). You can check if it works on your server by simply appending "/foo" to any PHP-URL on your site. If you see the output of your PHP, all is good. If you see a 404 error then your server configuration can't deal with such URLs.

滴情不沾 2024-11-24 16:05:07

在 picture_generator.php 文件中,您需要添加带有名称的标头。例如

header("Content-Disposition: attachment; filename=\"myfile.png\""); 

In your picture_generator.php file you need to add a header with the name. such as

header("Content-Disposition: attachment; filename=\"myfile.png\""); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文