PHP检测源图像URL链接是否导致“损坏”图像?

时间:2010-09-09 15:19:28

标签: php gd

假设您有一个缩略图生成器脚本,它接受URL形式的源图像。有没有办法检测源URL是否“损坏” - 是否不存在或导致非图像文件?


使用getimagesize()或其他PHP GD功能蛮力不是解决方案,因为欺骗性的杂散URL可能根本不是图像(http://example.com/malicious.exe或同一文件,但重命名为{{1可以输入 - 在必须调用GD之前,PHP可以很容易地检测到这种情况。我正在寻找GD预先消毒,然后让GD尝试解析文件。


作为第一步,以下正则表达式检查URL是否是图像扩展名: http://example.com/malicious.jpg

8 个答案:

答案 0 :(得分:11)

在php中使用file_exists函数,你可以用它检查网址。

请参阅下面的文档,说明如何检查img ...您需要的确切内容

文件存在 - http://www.php.net/manual/en/function.file-exists.php#93572

网址EXISTS - http://www.php.net/manual/en/function.file-exists.php#85246


以下alternative code用于检查网址。如果您要在浏览器中使用\n

替换<br/>
<?php

$urls = array('http://www.google.com/images/logos/ps_logo2.png', 'http://www.google.com/images/logos/ps_logo2_not_exists.png');

foreach($urls as $url){
   echo "$url - ";
   echo url_exists($url) ? "Exists" : 'Not Exists';
   echo "\n\n";
}


function url_exists($url) {
    $hdrs = @get_headers($url);

    echo @$hdrs[1]."\n";

    return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;
}
?>

输出如下

http://www.google.com/images/logos/ps_logo2.png - Content-Type: image/png
Exists

http://www.google.com/images/logos/ps_logo2_not_exists.png - Content-Type: text/html; charset=UTF-8
Not Exists

答案 1 :(得分:7)

我使用以下方法检测远程图像的属性

$src='http://example.com/image.jpg';
list($width, $height, $type, $attr) = @getimagesize($src);

示例(检查stackoverflows“Careers 2.0”图像)

$src='http://sstatic.net/ads/img/careers2-ad-header-so.png';
list($width, $height, $type, $attr) = @getimagesize($src);

echo '<pre>';
echo $width.'<br>';
echo $height.'<br>';
echo $type.'<br>';
echo $attr.'<br>';
echo '</pre>';

如果$ height,$ width等为null,则图像显然不是图像或文件不存在。使用cURL过度且速度较慢(即使使用CURLOPT_HEADER)

答案 2 :(得分:3)

唯一真正可靠的方法是使用file_get_contents()请求图片,并使用getimagesize()查找图片类型。

仅当getimagesize()返回有效的文件类型时,您是否可以依赖它实际上是有效的图像。

但是,这是非常重要的资源。

您可以考虑不进行任何服务器端检查,并将onerror JavaScript事件添加到已完成的图像资源中:

<img src="..." onerror="this.style.display = 'none'">

答案 3 :(得分:1)

try for local files

<?php 
if(file_exists($filename))
{
//do what you want
}
else
{
//give error that file does not exists
}
?>
外部域

$headers = @get_headers($url);
if (preg_match("|200|", $headers[0])) {
// file exists
} else {
// file doesn't exist
}

您也可以使用curl请求。

答案 4 :(得分:1)

破解或未找到图像链接的快速解决方案
我建议你不要使用getimagesize()因为它将首先下载图像然后它将检查图像大小+如果这不会成像然后它将抛出异常所以使用下面的代码

if(checkRemoteFile($imgurl))
{
//found url, its mean
echo "this is image";
}

function checkRemoteFile($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    // don't download content
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(curl_exec($ch)!==FALSE)
    {
        return true;
    }
    else
    {
        return false;
    }
}

注意: 此当前代码可帮助您识别损坏或未找到的网址图像,这无法帮助您识别图像类型或标题

答案 5 :(得分:0)

在将实际图像放入生成器之前,您可以检查HTTP响应的HTTP status code(应该是200)和Content-type header(图像/ png等)。

如果这两个前提条件没问题,在检索图像后,您可以在其上调用getimagesize(),看它是否中断,返回的MIME类型等。

答案 6 :(得分:0)

你尝试过file_get_contents()方法吗?

http://php.net/manual/en/function.file-get-contents.php

答案 7 :(得分:0)

onerror Event

如果加载图像时发生错误,请执行JavaScript:

如果在加载外部文件(例如文档或图像)时发生错误,则会触发onerror事件。

示例:

<!DOCTYPE html>
<html>
<body>

<img src="image.gif" onerror="myFunction()">

<p>A function is triggered if an error occurs when loading the image. The function shows an alert box with a text.
In this example we refer to an image that does not exist, therefore the onerror event occurs.</p>

<script>
function myFunction() {
  alert('The image could not be loaded.');
}
</script>

</body>
</html>