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

I want to know from the syntax if the URL is an image or not. Example: http://www.blah.com/plo52.jpg or png or gif will return true. if the URL ends with another extension, the function will return false.

thanks

+4
source share
8 answers

Check the mime file type.

+14
source

This will not tell you if this image is valid. This will only tell you what the URL seems to match:

$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 

Please note that if you expect to see images transmitted through .php scripts or .aspx scripts, this method will fail. To have a really reliable test, you need to check the mime type.

+8
source

I suggest making an HTTP HEAD request, so you won’t have to download the whole image, and then based on the line received from the parsing, and make sure that the Content-Type is image/jpeg image/pjpeg , image/gif , image/png or similar types of content.

 <?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, Jan 10, 2010 09:14:52 GMT

Accept-Ranges: bytes

ETag: "32741b5ed591ca1: 0"

Server: Microsoft-IIS / 7.5

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

'(length = 256)

The Content-Type header can be faked, of course ... but in 99% of cases it will not, so this method is reliable.

+5
source

Non-image URLs can still transmit backward images. For example, an ashx or asp url might return image data to you. The best way is to check the MIME type, Response.ContentType.

+3
source

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

+2
source

I approved the preg solution, but if you really REALLY want to find out, then upload the file and do a poll using system functions (for example, to use linux file via exec).

+1
source

You can use pathinfo to get the extension:

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

then wrap it in 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 with regular file paths, and if you can pass the $ extension array, you are not limited to the regular expression pattern. Please note that the function is case sensitive.

See this question and answers for the best way to identify a MimeType file :

+1
source

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

+1
source

All Articles