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.
Than开发者_开发知识库ks
Check for the mime-type of the file.
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.
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.
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.
If you really need to be 100% sure, you need to download the resource and check it using getimagesize().
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).
Use headers_list and then check the Content-Type http://php.net/manual/en/function.headers-list.php
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:
精彩评论