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.

5
  • Be sure to sanitize the URL if you do this! Or better yet, use curl, which can't (shouldn't?) access the filesystem.
    – strager
    Aug 8, 2010 at 9:01
  • 1
    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.
    – Blizz
    Aug 8, 2010 at 9:06
  • 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
    Aug 8, 2010 at 9:41
  • @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.
    – Blizz
    Aug 8, 2010 at 9:46
  • false positive with pictures.content4us.com/products_high_res/153907z.eps.JPG Sep 7, 2018 at 13:27
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.

0
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
  • First time I've seen ['mime']. Do you know of somewhere that I can read up more on what it is and does? Aug 20, 2014 at 16:47
  • @TrevorW I think there should be some information about it on the getimagesize function page php.net/manual/en/function.getimagesize.php
    – TURTLE
    Aug 20, 2014 at 19:33
  • 1
    Sadly, sometimes extensions lie :'( Feb 8, 2017 at 16:44
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
  • Great link for other methods too!
    – Arjan
    Aug 8, 2010 at 9:51
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;
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;
    }
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
  • 2
    Turn off E_NOTICE reporting first Why not use the @ operator if it's necessary?
    – strager
    Aug 8, 2010 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.