检查网址是否为图片

时间:2019-04-04 04:43:01

标签: php

我正在尝试检查url是否为图片,所以我确实使用了getheader; getheader在localhost上运行,但在托管服务器上不运行。我还尝试使用getimagesize,它也可以在localhost上运行,但即使在php.ini设置为

时,也不能在虚拟主机服务器上运行
allow_url_fopen = On
allow_url_include = On

对于get_header我的代码是

$url = "https://hgtvhome.sndimg.com/content/dam/images/hgtv/fullset/2016/2/16/2/Orig-Paul-Schultz_Toybox-Home-kitchen-1.jpg.rend.hgtvcom.616.411.suffix/1455654217545";

        $url_headers=get_headers($url, 1);

 if(isset($url_headers['Content-Type'])){

            $type=strtolower($url_headers['Content-Type']);

            $valid_image_type=array();
            $valid_image_type['image/png']='';
            $valid_image_type['image/jpg']='';
            $valid_image_type['image/jpeg']='';
            $valid_image_type['image/jpe']='';
            $valid_image_type['image/gif']='';
            $valid_image_type['image/tif']='';
            $valid_image_type['image/tiff']='';
            $valid_image_type['image/svg']='';
            $valid_image_type['image/ico']='';
            $valid_image_type['image/icon']='';
            $valid_image_type['image/x-icon']='';

            if(isset($valid_image_type[$type])){

                echo "Yes it's Images";

            }
            else
            {
                echo "no image";
            }
        }

在本地主机上显示的getheader的var_dump

array (size=11)
  0 => string 'HTTP/1.0 200 OK' (length=15)
  'Content-Type' => string 'image/jpeg' (length=10)
  'Server' => string 'Apache' (length=6)
  'X-Content-Type-Options' => string 'nosniff' (length=7)
  'X-Frame-Options' => string 'SAMEORIGIN' (length=10)
  'Content-Length' => string '38885' (length=5)
  'Cache-Control' => string 'max-age=2522004' (length=15)
  'Expires' => string 'Fri, 03 May 2019 08:55:29 GMT' (length=29)
  'Date' => string 'Thu, 04 Apr 2019 04:22:05 GMT' (length=29)
  'Connection' => string 'close' (length=5)
  'Vary' => string 'User-Agent' (length=10)

但是在虚拟主机服务器上显示var_dump

    Array
(
    [server] => AkamaiGHost, Apache
    [content-length] => 0, 2077
    [location] => https://www.foodnetwork.com/not-available.html
    [cache-control] => max-age=0, max-age=433
    [expires] => Thu, 04 Apr 2019 05:16:02 GMT, Thu, 04 Apr 2019 05:23:15 GMT
    [date] => Thu, 04 Apr 2019 05:16:02 GMT, Thu, 04 Apr 2019 05:16:02 GMT
    [connection] => keep-alive, keep-alive
    [vary] => User-Agent, User-Agent, Accept-Encoding
    [content-type] => text/html; charset=UTF-8
    [access-control-allow-method] => GET
    [server-timing] => edge; dur=0, cdn-cache; desc=HIT
    [x-akamai-transformed] => 9 - 0 pmb=mRUM,3
    [access-control-allow-origin] => *
    [content-encoding] => gzip
)

对于getimagesize,我的代码是

 if (@getimagesize($url)) {
    echo  "image exists ";
    } else {
    echo  "image does not exist ";
    }

也可以在localhost上运行,但不能在Web主机服务器上使用,@ getimagesize($ url)的var_dump显示为:

bool(false)

2 个答案:

答案 0 :(得分:1)

根据您的评论,您的图片网址不正确。应该是:https://hgtvhome.sndimg.com/content/dam/images/hgtv/fullset/2016/2/16/2/Orig-Paul-Schultz_Toybox-Home-kitchen-1.jpg

检查时应删除图像缓存和图像版本控制。

检查天气文件是否为图像的最佳解决方案

  

解决方案1 ​​

if(@is_array(getimagesize($mediapath))){
    $image = true;
} else {
    $image = false;
}
  

解决方案2

$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['fupload']['tmp_name']);
$error = !in_array($detectedType, $allowedTypes);
  

解决方案3

 $supported_image = array('gif','jpg','jpeg','png','bmp');

$src_file_name = 'https://hgtvhome.sndimg.com/content/dam/images/hgtv/fullset/2016/2/16/2/Orig-Paul-Schultz_Toybox-Home-kitchen-1.jpg';

$ext = strtolower(pathinfo($src_file_name, PATHINFO_EXTENSION));

 if (in_array($ext, $supported_image)){
    echo "it's image";
 }
 else {
    echo 'not image';
 }

答案 1 :(得分:0)

  1. 您应遵循重定向标头(代码30x)

  2. 在这种情况下,图像似乎“不可用”,因为重定向是指向不可用的页面。网址可能不正确?

  3. 您还应该注意的是:许多内容提供程序至少检查HTTP引荐来源标头,或者它们拒绝访问图像(使抓取更加困难)。其他人则检查先前设置的cookie。最后,有些人可以使用一些复杂的基于令牌的身份验证(我不会介绍)。但对于第一个问题以及重定向之后,您应该使用Curl:https://www.php.net/manual/en/book.curl.php

  4. 内容类型标头通常是不可靠的。我建议在结果索引2中使用getimagesize返回的exif图像类型,您似乎仍然在调用它。

回顾一下:使用Curl设置引荐来源网址并遵循重定向,并可能从您可能浏览的先前引荐来源页面传播cookie,一旦获得内容,请使用getimagesize确定类型。