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.