2012-01-10 53 views
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); 
} 

回答