'gif', IMAGETYPE_JPEG => 'jpg', IMAGETYPE_PNG => 'png', IMAGETYPE_TIFF_II => 'tiff', IMAGETYPE_TIFF_MM => 'tiff', ); protected $driver; protected $actions = array(); protected $image = ''; public static function factory($image, $config = NULL) { return new Image($image, $config); } public function __construct($image, $config = NULL) { static $check; ($check === NULL) and $check = function_exists('getimagesize'); if ($check === FALSE) throw new Kohana_Exception('image.getimagesize_missing'); if ( ! is_file($image)) throw new Kohana_Exception('image.file_not_found', $image); $ER = error_reporting(0); $image_info = getimagesize($image); error_reporting($ER); if ( ! is_array($image_info) OR count($image_info) < 3) throw new Kohana_Exception('image.file_unreadable', $image); if ( ! isset(Image::$allowed_types[$image_info[2]])) throw new Kohana_Exception('image.type_not_allowed', $image); $this->image = array ( 'file' => str_replace('\\', '/', realpath($image)), 'width' => $image_info[0], 'height' => $image_info[1], 'type' => $image_info[2], 'ext' => Image::$allowed_types[$image_info[2]], 'mime' => $image_info['mime'] ); $this->config = (array) $config + Kohana::config('image'); $driver = 'Image_'.ucfirst($this->config['driver']).'_Driver'; if ( ! Kohana::auto_load($driver)) throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this)); $this->driver = new $driver($this->config['params']); if ( ! ($this->driver instanceof Image_Driver)) throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Image_Driver'); } public function __get($property) { if (isset($this->image[$property])) { return $this->image[$property]; } else { throw new Kohana_Exception('core.invalid_property', $property, get_class($this)); } } public function resize($width, $height, $master = NULL) { if ( ! $this->valid_size('width', $width)) throw new Kohana_Exception('image.invalid_width', $width); if ( ! $this->valid_size('height', $height)) throw new Kohana_Exception('image.invalid_height', $height); if (empty($width) AND empty($height)) throw new Kohana_Exception('image.invalid_dimensions', __FUNCTION__); if ($master === NULL) { $master = Image::AUTO; } elseif ( ! $this->valid_size('master', $master)) throw new Kohana_Exception('image.invalid_master'); $this->actions['resize'] = array ( 'width' => $width, 'height' => $height, 'master' => $master, ); return $this; } public function crop($width, $height, $top = 'center', $left = 'center') { if ( ! $this->valid_size('width', $width)) throw new Kohana_Exception('image.invalid_width', $width); if ( ! $this->valid_size('height', $height)) throw new Kohana_Exception('image.invalid_height', $height); if ( ! $this->valid_size('top', $top)) throw new Kohana_Exception('image.invalid_top', $top); if ( ! $this->valid_size('left', $left)) throw new Kohana_Exception('image.invalid_left', $left); if (empty($width) AND empty($height)) throw new Kohana_Exception('image.invalid_dimensions', __FUNCTION__); $this->actions['crop'] = array ( 'width' => $width, 'height' => $height, 'top' => $top, 'left' => $left, ); return $this; } public function rotate($degrees) { $degrees = (int) $degrees; if ($degrees > 180) { do { $degrees -= 360; } while($degrees > 180); } if ($degrees < -180) { do { $degrees += 360; } while($degrees < -180); } $this->actions['rotate'] = $degrees; return $this; } public function flip($direction) { if ($direction !== Image::HORIZONTAL AND $direction !== Image::VERTICAL) throw new Kohana_Exception('image.invalid_flip'); $this->actions['flip'] = $direction; return $this; } public function quality($amount) { $this->actions['quality'] = max(1, min($amount, 100)); return $this; } public function sharpen($amount) { $this->actions['sharpen'] = max(1, min($amount, 100)); return $this; } public function save($new_image = FALSE, $chmod = 0644, $keep_actions = FALSE) { empty($new_image) and $new_image = $this->image['file']; $dir = pathinfo($new_image, PATHINFO_DIRNAME); $file = pathinfo($new_image, PATHINFO_BASENAME); $dir = str_replace('\\', '/', realpath($dir)).'/'; if ( ! is_writable($dir)) throw new Kohana_Exception('image.directory_unwritable', $dir); if ($status = $this->driver->process($this->image, $this->actions, $dir, $file)) { if ($chmod !== FALSE) { chmod($new_image, $chmod); } } if ($keep_actions === FALSE) $this->actions = array(); return $status; } public function render($keep_actions = FALSE) { $new_image = $this->image['file']; $dir = pathinfo($new_image, PATHINFO_DIRNAME); $file = pathinfo($new_image, PATHINFO_BASENAME); $dir = str_replace('\\', '/', realpath($dir)).'/'; $status = $this->driver->process($this->image, $this->actions, $dir, $file, $render = TRUE); if ($keep_actions === FALSE) $this->actions = array(); return $status; } protected function valid_size($type, & $value) { if (is_null($value)) return TRUE; if ( ! is_scalar($value)) return FALSE; switch ($type) { case 'width': case 'height': if (is_string($value) AND ! ctype_digit($value)) { if ( ! preg_match('/^[0-9]++%$/D', $value)) return FALSE; } else { $value = (int) $value; } break; case 'top': if (is_string($value) AND ! ctype_digit($value)) { if ( ! in_array($value, array('top', 'bottom', 'center'))) return FALSE; } else { $value = (int) $value; } break; case 'left': if (is_string($value) AND ! ctype_digit($value)) { if ( ! in_array($value, array('left', 'right', 'center'))) return FALSE; } else { $value = (int) $value; } break; case 'master': if ($value !== Image::NONE AND $value !== Image::AUTO AND $value !== Image::WIDTH AND $value !== Image::HEIGHT) return FALSE; break; } return TRUE; } }