i wrote this function:
function isImage($url)
{
$params = array('http' => array(
'method' => 'HEAD'
));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp)
return false; // Problem with url
$meta = stream_get_meta_data($fp);
if ($meta === false)
{
fclose($fp);
return false; // Problem reading data from url
}
$wrapper_data = $meta["wrapper_data"];
if(is_array($wrapper_data)){
foreach(array_keys($wrapper_data) as $hh){
if(substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") // strlen("Content-Type: image") == 19
{
fclose($fp);
return true;
}
}
}
fclose($fp);
return false;
}
It does checks if a url returns an image type, now i would like to extend this method controlling if returned image’s extension is in array (‘png’,’jpeg’,’jpg’).
i need this control, cause of users submitting image urls on my app.
Any suggestion will be appriciated!
This code snippet will return you the image type along with the checking if it’s an image :
If you call this function with this :
It will return
png.And if you call with this url :
It will return
Not an image.Hope that helps . Happy coding 🙂