检测 URL 是否为图像的最佳方法是什么?

发布于 2024-10-08 03:19:19 字数 28 浏览 0 评论 0原文

检测 URL 是否为图像的最佳方法是什么?

What is the best way to detect that a URL is that of an image?

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

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

发布评论

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

评论(5

別甾虛僞 2024-10-15 03:19:19

使用 PHP:

  • 最安全 - 获取实际图像数据。执行 getimagesize() 通过实际读取图像内的标头字节来确定图像类型文件。

  • 最快 - 获取资源、解析响应标头并查找 content-type 标头。此方法不如第一种方法可靠:服务器可能撒谎或配置错误。

  • 甚至更快 - 对 URL 执行 HEAD 请求,并查找 content-type 标头。可能不适用于动态图像资源。我没有这方面的经验,因此效果可能会有所不同。

  • 极度偏执最安全但速度慢 - 使用 PHP 获取实际图像数据,使用 GD 库将它们复制到新的图像资源中,然后保存结果。如果此过程成功,则保证是一个有效的、未受污染的图像。资源密集型;浏览器支持但 GD 不支持的一些有效图像格式将被列入表中。

我会选择第一个选择。

使用 JavaScript:

  • 将图像资源加载到 img 元素中。如果 onload 事件触发,并且图像随后具有物理尺寸,则浏览器成功加载该图像。

Using PHP:

  • Safest - fetch the actual image data. Do a getimagesize() to determine the image type by actually reading the header bytes inside the file.

  • Fastest - fetch the resource, parse the response headers and look for the content-type header. This method is not as reliable as the first one: The server could be lying, or misconfigured.

  • Even faster - do a HEAD request on the URL, and look for the content-type header. May not work for dynamic image resources. I have no experience with this, so mileage may vary.

  • Ultra-paranoid safest but slooow - fetch the actual image data using PHP, copy them into a new image resource using the GD library, and save the result. If this process works out, it is guaranteed to be a valid, untainted image. Resource-intensive; some valid image formats that a browser supports but GD does not will fall under the table.

I'd go for the first option.

Using JavaScript:

  • Load the image resource into an img element. If the onload event fires, and the image has physical dimensions afterwards, the browser managed to load the image.
请帮我爱他 2024-10-15 03:19:19

可以看看 HTTP 标头吗?只是一个想法。

May be look at the HTTP Headers? Just a thought.

浅浅淡淡 2024-10-15 03:19:19

一旦您请求该 URL 的 HTTP 连接,请尝试检查响应的标头...尝试查找 mime 类型。

Try to check the headers of the response once you have request an HTTP connection of that URL... try to look for the mime-type.

故事与诗 2024-10-15 03:19:19
  • 检查标头
  • 检查网址中的扩展名(. gif/.png 等)
  • inspect headers
  • check for extension in url(.gif/.png etc)
恍梦境° 2024-10-15 03:19:19

如果您只有 URL 而没有服务器在您请求 URL 时给您的响应,那么您唯一能做的就是根据文件扩展名进行猜测,但不能保证以 .jpg 结尾的文件是一个图像或以 .php 结尾的文件不是图像。

您可能会很高兴以 .jpg、.gif、.png 等结尾的网址将是图像,但不能保证。

对于其他任何事情来说,除非你检索它,否则基本上是不可能的。

If you only have the URL rather than the response the server will give you when you request the URL, then the only thing you can do is guess based on the file extension, but there is NO guarantee that a file ending in .jpg is an image or that a file ending in .php is not an image.

You can probably be reasonably happy urls ending in .jpg, .gif, .png etc are going to be images but it is not guaranteed.

For anything else its essentially impossible unless you retrive it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文