CodeOverload
CodeOverload

Reputation: 48475

How to determine if a url is an image or not?

i want to know from the syntax if a url is a picture or not. example: http://www.blah.com/plo52.jpg or png or gif will return true. if the url is ending with other extension, the function will return false.

Thanks

Upvotes: 4

Views: 2302

Answers (8)

Gordon
Gordon

Reputation: 316969

You can use pathinfo for getting the extension:

echo pathinfo('http://www.blah.com/plo52.jpg', PATHINFO_EXTENSION); // jpg

then wrap it into a function

function hasExtension($uri, array $extensions = array('jpg','gif','png'))
{
    return in_array(pathinfo($uri, PATHINFO_EXTENSION), $extensions);
}

hasExtension('http://www.blah.com/plo52.jpg'); // true

This will also work on regular file paths and by being able to pass the $extension array you are not limited to the regex pattern. Note the function is case sensitive though.

See this question and answers for how to best determine the MimeType of a file:

Upvotes: 1

meder omuraliev
meder omuraliev

Reputation: 186562

I suggest making a HTTP HEAD Request so you won't need to download the entire image, and then based on the string returned parse and make sure the Content-Type is an image/jpeg, image/pjpeg, image/gif, image/png or similar image Content-Types.

<?php

function parseImage( $url ) {
        $ch = curl_init();

    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

    curl_setopt( $ch, CURLOPT_HEADER, true );
    curl_setopt( $ch, CURLOPT_NOBODY, true );

    $content = curl_exec( $ch );

    var_dump($content);
    curl_close($ch);
}

parseImage('http://sstatic.net/so/img/logo.png');

Returns

string 'HTTP/1.1 200 OK

Cache-Control: max-age=604800

Content-Length: 3438

Content-Type: image/png

Last-Modified: Sun, 10 Jan 2010 09:14:52 GMT

Accept-Ranges: bytes

ETag: "32741b5ed591ca1:0"

Server: Microsoft-IIS/7.5

Date: Wed, 13 Jan 2010 20:37:47 GMT

' (length=256)

The Content-Type header can be spoofed, sure.. but 99% of the time it won't be, which is why this method is reliable.

Upvotes: 5

easement
easement

Reputation: 6139

Use headers_list and then check the Content-Type http://php.net/manual/en/function.headers-list.php

Upvotes: 1

Sampson
Sampson

Reputation: 268344

This won't tell you if it's really an image. This will only tell you what it appears to be according to the url:

$url = "http://somedomain.com/images/kittens.jpg";
if(preg_match("/\.(png|jpeg|jpg|gif|bmp)$/i", $url)) {
  print "Appears to be an image";
} else {
  print "Not an image.";
}

Outputs:

    Appears to be an image

Note that if you expect to see images fed through .php scripts or .aspx scripts, this method will fail. To have a truly-reliable test, you'll need to check the mime-type.

Upvotes: 8

Pekka
Pekka

Reputation: 449415

If you really need to be 100% sure, you need to download the resource and check it using getimagesize().

Upvotes: 2

Mike
Mike

Reputation: 2593

I upvoted the preg solution, but if you really, REALLY want to know then download the file and interrogate using system functions (e.g. for linux use file via exec).

Upvotes: 1

Matthew Cole
Matthew Cole

Reputation: 1339

Non-image URLs could still stream back images. For example, an ashx or asp URL could return you image data. The best way is to check the MIME type, Response.ContentType.

Upvotes: 3

kjagiello
kjagiello

Reputation: 8410

Check for the mime-type of the file.

Upvotes: 14

Related Questions