Giffary
Giffary

Reputation: 3128

how to check if a url is an image url with php?

I need to check the url is image url or not? How can i do this?

Examples :

Upvotes: 13

Views: 47232

Answers (7)

nikksan
nikksan

Reputation: 3471

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.

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

Upvotes: 3

TURTLE
TURTLE

Reputation: 3847

You can check if a url is an image by using the getimagesize function like below.

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.';

Upvotes: 6

Serega Lan
Serega Lan

Reputation: 41

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;

Upvotes: 3

Haim Evgi
Haim Evgi

Reputation: 125486

i think that the idea is to get a content of the header url via curl

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

look how to do it in this link :

http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_content_type#IfyouareusingCURL

Upvotes: 3

VolkerK
VolkerK

Reputation: 96159

You can send a HEAD request to the server and then check the Content-type. This way you at least know what the server "thinks" what the type is.

Upvotes: 12

Blizz
Blizz

Reputation: 8400

If you want to be absolutely sure, and your PHP is enabled for remote connections, you can just use

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.

Upvotes: 29

Misiur
Misiur

Reputation: 5297

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

Upvotes: 0

Related Questions