Via curl from url i getting some info and i need to check if it is an image.
$result = curl_exec($ch);
UPDATE: Content-type checking is not a good idea, because it can be faked.
Via curl from url i getting some info and i need to check if it is an image.
$result = curl_exec($ch);
UPDATE: Content-type checking is not a good idea, because it can be faked.
I would personally use gd tools within PHP to check if its an image or not. You cannot trust that the source gives the right MIME within the header. More than once I have trusted the headers and been disappionted by the fact that the content was an image but was transferred (due to the way the url/server works) over another format.
function getContentType($url)
{
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => $url ) );
curl_exec( $curl ); //execute
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); //get content type
curl_close( $curl );
return $contentType;
}
The above function will return you the type and then u can check for substring image
in value returned
I guess one way would be to read the HTTP-headers, especially the Content-type
header, and evaluate whether it is an image or not.
This SO question discuss how to check http headers using curl.
Use this to get the MIME type.
echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
and use it against common image mime types viz. image/gif, image/png, etc.
CURLOPT_HEADER
via curl_setopt.You might also want to set the request method to HEAD by setting CURLOPT_NOBODY
if you are only interested in the content type.
$c = curl_init();
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'HEAD' );
curl_setopt( $c, CURLOPT_HEADER, 1 );
curl_setopt( $c, CURLOPT_NOBODY, true );
curl_setopt( $c, CURLOPT_URL, 'your.url' );
curl_exec($c);
$content_type = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
And check for allowed content-type.
You can use getimagesize
<?php
$i = getimagesize('http://static.adzerk.net/Advertisers/bd294ce7ff4c43b6aad4aa4169fb819b.jpg');
print_r($i);
Output
Array
(
[0] => 220
[1] => 250
[2] => 2
[3] => width="220" height="250"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
In case its not image you'll get false
$i = getimagesize('http://stackoverflow.com');
var_dump($i);
Output:
bool(false)