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

查看:93
本文介绍了确定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.

以下是我目前所拥有的:

Here is what I currently have:

  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有一个文章,展示了如何使用此方法发送帖子请求,但它可以很容易地适应发送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天全站免登陆