Created
February 8, 2013 12:17
-
-
Save mattattui/4738639 to your computer and use it in GitHub Desktop.
3 ways to test if a file is an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Test if given file is a web image (gif/jpg/png) | |
* | |
* Requires PHP 5.3 or newer (builtin) or the Fileinfo PECL extension. | |
* | |
* @param string $file Path to image | |
* @return boolean True if web image, or false if unreadable or not image | |
*/ | |
function finfo_is_image($file) | |
{ | |
if (!is_readable($file)) { | |
return false; | |
} | |
$finfo = new Finfo(FILEINFO_MIME_TYPE); | |
$type = $finfo->file($file); | |
switch($type) { | |
case 'image/jpeg': | |
case 'image/png': | |
case 'image/gif': | |
return true; | |
default: | |
return false; | |
} | |
} | |
/** | |
* Test if given file is a web image (gif/jpg/png) | |
* | |
* Uses the getimagesize() built-in function. Don't use it on a URL because it | |
* will download the whole image before checking. | |
* | |
* @param string $file Path to image | |
* @return mixed Returns "gif", "jpg", "png", or false if unreadable or not image | |
*/ | |
function getimagesize_is_image($file) | |
{ | |
if (!(is_file($file) && is_readable($file))) { | |
return false; | |
} | |
$info = getimagesize($file); | |
if (!is_array($info)) { | |
return false; | |
} | |
switch($info[2]) { | |
case IMAGETYPE_JPEG: | |
case IMAGETYPE_PNG: | |
case IMAGETYPE_GIF: | |
return true; | |
default: | |
return false; | |
} | |
} | |
/** | |
* Test if given file is a web image (gif/jpg/png) | |
* | |
* More URL-safe than getimagesize method & works on any PHP version. | |
* | |
* @param string $image_path Path to image | |
* @return boolean True if web image, or false if unreadable or not image | |
*/ | |
function fopen_is_image($image_path) | |
{ | |
if (!(is_file($image_path) && is_readable($image_path))) { | |
return false; | |
} | |
if (!$f = fopen($image_path, 'rb')) | |
{ | |
return false; | |
} | |
$data = fread($f, 8); | |
fclose($f); | |
// signature checking | |
$unpacked = unpack("H12", $data); | |
if (array_pop($unpacked) == '474946383961' || array_pop($unpacked) == '474946383761') return true; | |
$unpacked = unpack("H4", $data); | |
if (array_pop($unpacked) == 'ffd8') return true; | |
$unpacked = unpack("H16", $data); | |
if (array_pop($unpacked) == '89504e470d0a1a0a') return true; | |
return false; | |
} | |
// Tests: | |
$tests = array( | |
'/Users/matt/Downloads/cougar_6.jpg' => true, | |
'/Users/matt/Downloads/IMG_16102012_161427.png' => true, | |
'/Users/matt/Downloads/691493.gif' => true, | |
'/Users/matt/Downloads/Stranger-in-moscow.mp3' => false, | |
'/filenotfound' => false, | |
'/tmp/unreadable' => false, | |
'/tmp' => false, | |
); | |
echo "finfo_is_image():".PHP_EOL; | |
foreach ($tests as $file => $expected) { | |
echo $file . '...'; | |
if ($expected) { | |
echo assert(finfo_is_image($file)) ? 'OK' : 'FAILED'; | |
} else { | |
echo assert(!finfo_is_image($file)) ? 'OK' : 'FAILED'; | |
} | |
echo PHP_EOL; | |
} | |
echo PHP_EOL."getimagesize_is_image():".PHP_EOL; | |
foreach ($tests as $file => $expected) { | |
echo $file . '...'; | |
if ($expected) { | |
echo assert(getimagesize_is_image($file)) ? 'OK' : 'FAILED'; | |
} else { | |
echo assert(!getimagesize_is_image($file)) ? 'OK' : 'FAILED'; | |
} | |
echo PHP_EOL; | |
} | |
echo PHP_EOL."fopen_is_image():".PHP_EOL; | |
foreach ($tests as $file => $expected) { | |
echo $file . '...'; | |
if ($expected) { | |
echo assert(fopen_is_image($file)) ? 'OK' : 'FAILED'; | |
} else { | |
echo assert(!fopen_is_image($file)) ? 'OK' : 'FAILED'; | |
} | |
echo PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment