php isImage($url)使用stream_get_meta_data;无法使用本地文件路径


php isImage($url) using stream_get_meta_data doesn't work with local file path

是否可以调整此函数,使其也适用于本地文件路径?

$meta包含以下内容:阵列([wrapper_type]=>普通文件[stream_type]=>STDIO[mode]=>rb[unread_bytes]=>0[seeable]=>1[uri]=>C:''imgs''232434.jpg[timed_out]=>[blocked]=>1[iof]=>)

$meta["wrapper_data"]不存在。

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;
  }

最好使用getimagesize来检测本地/URL文件是否为图像。

失败时,返回FALSE。这可能就是你想要的。