/** * Validation rule to test if an upload is an image and, optionally, is the correct size. * * // The "image" file must be an image * $array->rule('image', 'Upload::image') * * // The "photo" file has a maximum size of 640x480 pixels * $array->rule('photo', 'Upload::image', array(':value', 640, 480)); * * // The "image" file must be exactly 100x100 pixels * $array->rule('image', 'Upload::image', array(':value', 100, 100, TRUE)); * * * @param array $file $_FILES item * @param integer $max_width maximum width of image * @param integer $max_height maximum height of image * @param boolean $exact match width and height exactly? * @return boolean */ public static function image(array $file, $max_width = NULL, $max_height = NULL, $exact = FALSE) { if (Upload::not_empty($file)) { try { // Get the width and height from the uploaded image list($width, $height) = getimagesize($file['tmp_name']); } catch (ErrorException $e) { // Ignore read errors } if (empty($width) or empty($height)) { // Cannot get image size, cannot validate return FALSE; } if (!$max_width) { // No limit, use the image width $max_width = $width; } if (!$max_height) { // No limit, use the image height $max_height = $height; } if ($exact) { // Check if dimensions match exactly return $width === $max_width and $height === $max_height; } else { // Check if size is within maximum dimensions return $width <= $max_width and $height <= $max_height; } } return FALSE; }
public function uploaded($name) { return isset($_FILES[$name]) and Upload::not_empty($_FILES[$name]) and Upload::valid($_FILES[$name]); }