0
我有一个PHP函数下面用于优化图像的命令行工具,在下面的函数运行ImageMagick传递给它的所有文件并获取文件扩展名。ImageMagick给非图像传递错误
我只在实际图像文件上测试过,直到今晚。我在混合的图像和其他文件的文件夹上进行测试。
的结果是这样的错误...
identify.exe: no decode delegate for this image format
`E:\Server\img\testfiles\archive.php' @ error/constitute.c/ReadImage/532.
现在我知道这是一种难以过滤非图像文件出当这个函数是专门用于确定这个值的时候......但是有没有更好的方法可以处理这种情况,如果一个非图像文件通过我的程序传递给这个函数,我可以抑制错误还是避免它们发生?
功能...
/**
* is_image
* @param mixed $file_path
* @static
* @access public
* @return void
*/
public static function is_image($file_path)
{
if (!file_exists($file_path)) {
throw new FileNotFoundException("File or path not found: " . $file_path);
}
$types = array("gif", "png", "gifgif", "jpg", "jpeg", "bmp");
//exec("/usr/bin/identify -quiet -format \"%m\" $file_path", $return, $error);
$imagemagick = self::$program_paths['imagemagick'] . '\identify';
exec($imagemagick . ' -quiet -format %m ' . $file_path, $return, $error);
$type = ($error === 0) ? mb_strtolower($return[0]) : "";
if ($error == 1) {
return false;
}
if (substr($type, 0, 6) === "gifgif") {
$type = "gifgif";
}
return in_array($type, $types);
}