13

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

Examples :

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Giffary
  • 3,060
  • 12
  • 50
  • 71
  • possible duplicate of [Check whether image exists on remote URL](http://stackoverflow.com/questions/1363925/check-whether-image-exists-on-remote-url) and on a more general level [How can one check to see if a remote file exists using PHP?](http://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php) – Gordon Aug 08 '10 at 09:13
  • 1
    possible duplicate of [best way to determine if a URL is an image in PHP](http://stackoverflow.com/questions/676949/best-way-to-determine-if-a-url-is-an-image-in-php) – Wipqozn Jan 14 '14 at 13:24

7 Answers7

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.

Blizz
  • 8,082
  • 2
  • 33
  • 53
  • 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 08 '10 at 09: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 08 '10 at 09: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 08 '10 at 09: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 08 '10 at 09:46
  • false positive with https://pictures.content4us.com/products_high_res/153907z.eps.JPG – pmiguelpinto90 Sep 07 '18 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.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
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.';
TURTLE
  • 3,728
  • 4
  • 49
  • 50
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

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
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;
    }
nikksan
  • 3,341
  • 3
  • 22
  • 27
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;
bstpierre
  • 30,042
  • 15
  • 70
  • 103
Serega Lan
  • 41
  • 4
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
}
Misiur
  • 5,019
  • 8
  • 38
  • 54