You can use getimagesize ()
$url = 'http://www.geenstijl.nl/archives/images/HassVivaCatFight.jpg'; $file = file_get_contents($url); $tmpfname = tempnam("/tmp", "FOO"); $handle = fopen($tmpfname, "w"); fwrite($handle, $file); $size = getimagesize($tmpfname); if(($size['mime'] == 'image/png') || ($size['mime'] == 'image/jpeg')){ //do something with the $file echo 'yes an jpeg of png'; } else{ echo 'Not an jpeg of png ' . $tmpfname .' '. $size['mime']; fclose($handle); }
I just tested it to make it work. You need to create a temporary file because the image functions work with local data, and they only accept the path to the local directory, for example, "C: \ wamp2 \ www \ temp \ image.png"
If you do not use fclose($handle); , PHP will automatically remove tmp after the script completes.
source share