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);
}