/** * Return image info hash. If image is not detected and abort=false, return * width=height=0 and suffix=mime=file=''. * * @param string $file * @param boolean $abort * @return map (width, height, mime, suffix, file) */ public static function imageInfo($file, $abort = true) { FSEntry::isFile($file); $info = getimagesize($file); $res = array('width' => 0, 'height' => 0, 'mime' => '', 'suffix' => '', 'file' => ''); if ($info === false || empty($info[0]) || empty($info[1]) || empty($info[2]) || empty($info['mime'])) { if ($abort) { throw new Exception('invalid image', $file . ': ' . print_r($info, true)); } else { return $res; } } if ($info[0] < 1 || $info[0] > 40000) { throw new Exception('invalid image width', $file . ': ' . $info[0]); } if ($info[1] < 1 || $info[1] > 40000) { throw new Exception('invalid image heigth', $file . ': ' . $info[1]); } $res['file'] = $file; $res['width'] = $info[0]; $res['height'] = $info[1]; $res['mime'] = $info['mime']; $mime_suffix = array('image/png' => '.png', 'image/jpeg' => '.jpg', 'image/gif' => '.gif', 'image/tiff' => '.tif', 'image/jp2' => '.jp2', 'image/jpx' => '.jpx', 'image/x-ms-bmp' => '.bmp', 'image/x-photoshop' => '.psd', 'image/x-xbitmap' => '.xbm'); $suffix_map = array(1 => '.gif', 2 => '.jpg', 3 => '.png', 4 => '.swf', 5 => '.psd', 6 => '.bmp', 7 => '.tif', 8 => '.tif', 9 => '.jpc', 10 => '.jp2', 11 => '.jpx', 12 => '.jb2', 13 => '.swc', 14 => '.iff', 15 => '.bmp', 16 => '.xbm', 17 => '.ico'); if (isset($mime_suffix[$info['mime']])) { $res['suffix'] = $mime_suffix[$info['mime']]; } else { if (isset($suffix_map[$info[2]])) { $res['suffix'] = $suffix_map[$info[2]]; } else { $suffix = File::suffix($file, true); if ($suffix == '.jpeg') { $suffix = '.jpg'; } else { if ($suffix == '.tiff') { $suffix = '.tif'; } } $res['suffix'] = $suffix; } } return $res; }
/** * Return files in directory with suffix in suffix_list. * * @collect: split basename at [_] and return hash { basename: [file, file2, ...], ... } * * @param string $path * @param array $suffix_list e.g. (jpg,png) or (.jpg,.png) - default = [] * @param string $rel_dir (default = '') - if set remove rel_dir in every entry * @return array */ public static function scanDir($path, $suffix_list = array(), $rel_dir = '') { self::_fix_suffix_list($suffix_list); $entries = Dir::entries($path); $found = array(); foreach ($entries as $entry) { if (FSEntry::isFile($entry, false) && in_array(File::suffix($entry), $suffix_list)) { if ($rel_dir) { array_push($found, str_replace($rel_dir, '', $entry)); } else { array_push($found, $entry); } } } return $found; }