最近寫的程式會處理圖片上傳下載的動作,通常都會各別使用到pathinfo()、filesize()、getimagesize(),但是每次都要各別呼叫有點麻煩,所以就寫了一個helper來一次解決這個問題,這個function是針對Kohana寫的,如果你不是使用KHN的話,你可以複製程式碼後,自行修改符合你需要的部分

/*
Array
(
    [dirname] => .
    [basename] => bee.jpg
    [extension] => jpg
    [filename] => bee
    [filesize] => 883496
    [width] => 1920
    [height] => 1200
    [0] => 1920
    [1] => 1200
    [2] => 2
    [3] => width="1920" height="1200"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)
*/
 class file extends file_Core {
/**
 * Info
 *
 * Get the File detail Info
 *
 * @access	public
 * @param	string 
 */	 
	public static function info($filename=NULL) {
		$imgaeType = array('jpg','gif','png');
		if(file_exists($filename)) {
			if(is_file($filename)) {
				//get basic info
				$info = pathinfo($filename);
				//add file size
				$info['filesize'] = filesize($filename);
				//add image info
				if(in_array($info['extension'],$imgaeType)) {
					$imageInfo = getimagesize($filename);
					$info['width'] = $imageInfo[0];
					$info['height'] = $imageInfo[1];
					//return info
					return array_merge($info,$imageInfo);
				} else {
					return $info;
				}
			} else {
				die($filename.' is not file!!');
			}
		} else {
			die($filename.' is not exists!!');
		}
	}
}

另外在檔案size的部分,之後可以配合Kohana的helper中的text::bytes()格式化適合閱讀的文字 / v \

arrow
arrow
    全站熱搜

    低溫烘培 發表在 痞客邦 留言(0) 人氣()