13

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

Examples :

2

7 Answers 7

29

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Be sure to sanitize the URL if you do this! Or better yet, use curl, which can't (shouldn't?) access the filesystem.
This doesn't executes commandos, it opens up the file and reads the header of the file. Can you explain me where the danger lies if someone would give say like /etc/passwd as the link? The function would just return FALSE.
It's a security risk nonetheless. What if getimagesize contained a bug? What if @Giffary decided to get more information from the image, and this gave more information to a potential hacker? I don't know, but IMO it's better to be safe than sorry.
@Strager I couldn't agree more about better being safe than sorry, it's way too easy to leave a security hole. I was just wondering if there's a particular reason you made that statement.
12

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.

Comments

6

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

3 Comments

First time I've seen ['mime']. Do you know of somewhere that I can read up more on what it is and does?
@TrevorW I think there should be some information about it on the getimagesize function page php.net/manual/en/function.getimagesize.php
Sadly, sometimes extensions lie :'(
3

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

1 Comment

Great link for other methods too!
3

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;

Comments

3

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

Comments

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

1 Comment

Turn off E_NOTICE reporting first Why not use the @ operator if it's necessary?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.