php 如何使用php检查一个url是否是一个图像url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3433640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check if a url is an image url with php?
提问by Giffary
I need to check the url is image url or not? How can i do this?
我需要检查网址是不是图片网址?我怎样才能做到这一点?
Examples :
例子 :
http://www.google.com/is notan image url.http://www.hoax-slayer.com/images/worlds-strongest-dog.jpgisan image url.https://stackoverflow.com/search?q=.jpgis notan image url.http://www.google.com/profiles/c/photos/private/AIbEiAIAAABECK386sLjh92M4AEiC3ZjYXJkX3Bob3RvKigyOTEzMmFmMDI5ODQ3MzQxNWQxY2VlYjYwYmE2ZTA4YzFhNDhlMjBmMAEFQ7chSa4PMFM0qw02kilNVE1Hpwisan image url.
http://www.google.com/不是图片网址。http://www.hoax-slayer.com/images/worlds-strongest-dog.jpg是图片网址。https://stackoverflow.com/search?q=.jpg不是图片网址。http://www.google.com/profiles/c/photos/private/AIbEiAIAAABECK386sLjh92M4AEiC3ZjYXJkX3Bob3RvKigyOTEzMmFmMDI5ODQ3MzQxNWQxY2VlYjYwYmE2ZTA4YzFhNDhlMjBmMAEFQ7chSa4PMFM0qw02kilNVE1Hpw是图片网址。
回答by Blizz
If you want to be absolutely sure, and your PHP is enabled for remote connections, you can just use
如果您想绝对确定,并且您的 PHP 已启用远程连接,则可以使用
getimagesize('url');
If it returns an array, it is an image type recognized by PHP, even if the image extension is not in the url (per your second link). You have to keep in mind that this method will make a remote connection for each request, so perhaps cache urls that you already probed in a database to lower connections.
如果它返回一个数组,则它是 PHP 识别的图像类型,即使图像扩展名不在 url 中(根据您的第二个链接)。您必须记住,此方法将为每个请求建立远程连接,因此可能会缓存您已经在数据库中探测到的 url 以降低连接。
回答by VolkerK
You can send a HEAD requestto the server and then check the Content-type. This way you at least know what the server "thinks" what the type is.
您可以向服务器发送HEAD 请求,然后检查内容类型。这样你至少知道服务器“认为”什么类型是什么。
回答by Jake
You can check if a url is an image by using the getimagesize function like below.
您可以使用如下所示的 getimagesize 函数检查 url 是否为图像。
function validImage($file) {
$size = getimagesize($file);
return (strtolower(substr($size['mime'], 0, 5)) == 'image' ? true : false);
}
$image = validImage('http://www.example.com/image.jpg');
echo 'this image ' . ($image ? ' is' : ' is not') . ' an image file.';
回答by Haim Evgi
i think that the idea is to get a content of the header url via curl
我认为这个想法是通过 curl 获取标题 url 的内容
and check the headers
并检查标题
After calling curl_exec()to get a web page, call curl_getinfo()to get the content type string from the HTTP header
调用curl_exec()获取网页后,调用curl_getinfo()从HTTP头中获取内容类型字符串
look how to do it in this link :
在此链接中查看如何操作:
http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_content_type#IfyouareusingCURL
http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_content_type#IfyouareusingCURL
回答by Serega Lan
Can use this:
可以用这个:
$is = @getimagesize ($link);
if ( !$is ) $link='';
elseif ( !in_array($is[2], array(1,2,3)) ) $link='';
elseif ( ($is['bits']>=8) ) $srcs[] = $link;
回答by nikksan
Here is a way that requires curl, but is faster than getimagesize, as it does not download the whole image. Disclaimer: it checks the headers, and they are not always correct.
这是一种需要 curl 的方法,但比 getimagesize 更快,因为它不会下载整个图像。免责声明:它检查标题,它们并不总是正确的。
function is_url_image($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$output = curl_exec($ch);
curl_close($ch);
$headers = array();
foreach(explode("\n",$output) as $line){
$parts = explode(':' ,$line);
if(count($parts) == 2){
$headers[trim($parts[0])] = trim($parts[1]);
}
}
return isset($headers["Content-Type"]) && strpos($headers['Content-Type'], 'image/') === 0;
}
回答by Misiur
$ext = strtolower(end(explode('.', $filename)));
switch($ext)
{
case 'jpg':
///Blah
break;
}
Hard version (just trying)
硬版本(只是尝试)
//Turn off E_NOTICE reporting first
if(getimagesize($url) !== false)
{
//Image
}

