【问题标题】:PHP control if image url is referring to specific image EXT [closed]如果图像 url 是指特定图像 EXT [关闭],则 PHP 控制
【发布时间】:2012-10-29 21:33:12
【问题描述】:

我写了这个函数:

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

它确实会检查一个 url 是否返回一个图像类型,现在我想扩展这个方法来控制返回的图像的扩展名是否在数组中 ('png','jpeg','jpg')。

我需要这个控制,因为用户在我的应用程序上提交图像 url。

任何建议都会被采纳!

【问题讨论】:

标签: php image validation url mime-types


【解决方案1】:

这段代码 sn-p 将返回图像类型以及检查它是否是图像:

    function get_type ($url) {

    $mimes = array (    'bmp'   =>  array('image/bmp', 'image/x-windows-bmp'),
                    'gif'   =>  'image/gif',
                    'jpeg'  =>  array('image/jpeg', 'image/pjpeg'),
                    'jpg'   =>  array('image/jpeg', 'image/pjpeg'),
                    'jpe'   =>  array('image/jpeg', 'image/pjpeg'),
                    'png'   =>  array('image/png',  'image/x-png')
            ) ;

    $info  = getimagesize($url);

    //$info['mime'] = 'image/gif';

    if ($info['mime']) {
        if ($type = array_search ($info ['mime'],$mimes)) 
            return $type;
        else {
            foreach ($mimes as $key=>$value) {
                if (is_array ($value) ) {
                    if ( !( FALSE === array_search ($info['mime'] , $value ) ))
                        return $key;
                }
            }
        }//end of else
    }
    else return "Not an image";

}

如果你用 this 调用这个函数:

echo get_type("http://www.gravatar.com/avatar/701467343cbd095506ca24b1ee5406d8?s=128&d=identicon&r=PG");

它将返回png

如果你用这个网址打电话:

echo get_type("http://www.google.com");

它将返回Not an image

希望对您有所帮助。快乐编码:)

【讨论】:

  • 好吧,我想我可以用你给我看的方法完全替换我的方法,因为实际上,如果你的方法没有返回 ext,那么 url 根本不会返回图像(与我的应用程序有关显然需要)
  • 很高兴,陛下 :)
  • eheheh,叫我ispuk 叫我ispuk :D
【解决方案2】:

检查“Content-Type”标头是否是其中之一:

  1. 内容类型:图片/png
  2. 内容类型:图像/JPEG
  3. 内容类型:图片/jpg

检查$url 参数是否以“.png”、“.jpg”或“.jpeg”结尾

【讨论】:

    【解决方案3】:

    如果我说对了,这段代码 sn-p 可以帮助你:

    $info  = getimagesize("http://www.gravatar.com/avatar/701467343cbd095506ca24b1ee5406d8?s=128&d=identicon&r=PG");
    echo $info['mime'];
    

    【讨论】:

      【解决方案4】:

      我个人更喜欢这样的preg_match

          $allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
          if(preg_match("/\." . $allowed_file_types . "$/i", 'image.jpg'))
          {
               // The file extention is allowed
          }
          else
          {
              // Not allowed!
          }
      

      【讨论】:

      • 是的,但是如果提交 site.com/image.png.gif 呢?
      【解决方案5】:

      要从某个 url 获取有关文件的信息,您可以使用以下代码:

      <?php
      //the request
      $ch = curl_init('http://www.google.com');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_exec($ch);
      
      // get the content type
      echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
      ?>
      

      将输出: 文本/html; charset=ISO-8859-1

      【讨论】:

        猜你喜欢
        • 2011-12-08
        • 2014-02-25
        • 1970-01-01
        • 2013-05-11
        • 1970-01-01
        • 2012-10-11
        • 2015-03-16
        • 2013-05-17
        • 1970-01-01
        相关资源
        最近更新 更多