确定 URL 是否是 PHP 中图像的最佳方法 [英] best way to determine if a URL is an image in PHP

查看:25
本文介绍了确定 URL 是否是 PHP 中图像的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP,给定一个URL,如何判断是否是图片?

Using PHP, given a URL, how can I determine whether it is an image?

URL 没有上下文——它只是在一个纯文本文件的中间,或者可能只是一个单独的字符串.

There is no context for the URL - it is just in the middle of a plain text file, or maybe just a string on its own.

我不想要高开销(例如读取 URL 的内容),因为这可能会被页面上的许多 URL 调用.鉴于此限制,识别所有图像并不是必需的,但我想要一个相当好的猜测.

I don't want high overhead (e.g. reading the content of the URL) as this could be called for many URLs on a page. Given this restriction, it isn't essential that all images are identified, but I would like a fairly good guess.

目前我只是在看文件扩展名,但感觉应该有比这个更好的方法.

At the moment I am just looking at the file extension, but it feels like there should be a better way than this.

这是我目前拥有的:

  function isImage( $url )
  {
    $pos = strrpos( $url, ".");
    if ($pos === false)
      return false;
    $ext = strtolower(trim(substr( $url, $pos)));
    $imgExts = array(".gif", ".jpg", ".jpeg", ".png", ".tiff", ".tif"); // this is far from complete but that's always going to be the case...
    if ( in_array($ext, $imgExts) )
      return true;
    return false;
  }

如果它对其他人有用,这里是使用 Emil H 回答中的技术的最终函数:

In case it's useful to anybody else here is the final function using the technique from Emil H's answer:

  function isImage($url)
  {
     $params = array('http' => array(
                  'method' => 'HEAD'
               ));
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) 
        return false;  // Problem with url

    $meta = stream_get_meta_data($fp);
    if ($meta === false)
    {
        fclose($fp);
        return false;  // Problem reading data from url
    }

    $wrapper_data = $meta["wrapper_data"];
    if(is_array($wrapper_data)){
      foreach(array_keys($wrapper_data) as $hh){
          if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") // strlen("Content-Type: image") == 19 
          {
            fclose($fp);
            return true;
          }
      }
    }

    fclose($fp);
    return false;
  }

推荐答案

您可以使用 HTTP HEAD 请求并检查内容类型.这可能是一个很好的妥协.可以使用 PHP Streams 来完成.Wez Furlong 有一篇文章,展示了如何使用这种方法发送 post 请求,但它可以很容易地改为发送 HEAD 请求.您可以使用 stream_get_meta_data() 从 http 响应中检索标头.

You could use an HTTP HEAD request and check the content-type. This might be a good compromise. It can be done using PHP Streams. Wez Furlong has an article that shows how to use this approach to send post requests, but it can be easily adapted to send HEAD requests instead. You can retrieve the headers from an http response using stream_get_meta_data().

当然,这并不是真正的 100%.一些服务器发送不正确的标头.但是,它将处理通过脚本传送图像并且正确的文件扩展名不可用的情况.真正确定的唯一方法是实际检索图像 - 全部或前几个字节,如 thomasrutter 所建议的那样.

Of course this isn't really 100%. Some servers send incorrect headers. It will however handle cases where images are delivered through a script and the correct file extension isn't available. The only way to be really certain is to actually retrieve the image - either all of it, or the first few bytes, as suggested by thomasrutter.

这篇关于确定 URL 是否是 PHP 中图像的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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