diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-11-24 19:20:36 -0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-11-24 19:20:36 -0800 |
commit | 9b6663f87a7e679ffba691cf516191fc840cf978 (patch) | |
tree | 20cf9f3aaf93b4ba69d282dcf10d259db4a752de /system/libraries/Image.php | |
parent | 82ee5f9d338017c69331b2907f37a468ced8c66e (diff) |
Update to Kohana r4684 which is now Kohana 2.4 and has substantial
changes.
Diffstat (limited to 'system/libraries/Image.php')
-rw-r--r-- | system/libraries/Image.php | 103 |
1 files changed, 73 insertions, 30 deletions
diff --git a/system/libraries/Image.php b/system/libraries/Image.php index 08c2957c..dd1e28ab 100644 --- a/system/libraries/Image.php +++ b/system/libraries/Image.php @@ -3,12 +3,12 @@ * Manipulate images using standard methods such as resize, crop, rotate, etc. * This class must be re-initialized for every image you wish to manipulate. * - * $Id: Image.php 4072 2009-03-13 17:20:38Z jheathco $ + * $Id: Image.php 4679 2009-11-10 01:45:52Z isaiah $ * * @package Image * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html + * @copyright (c) 2007-2009 Kohana Team + * @license http://kohanaphp.com/license */ class Image_Core { @@ -17,10 +17,16 @@ class Image_Core { const AUTO = 2; const HEIGHT = 3; const WIDTH = 4; + // Flip Directions const HORIZONTAL = 5; const VERTICAL = 6; + // Orientations + const PORTRAIT = 7; + const LANDSCAPE = 8; + const SQUARE = 9; + // Allowed image types public static $allowed_types = array ( @@ -68,11 +74,11 @@ class Image_Core { ($check === NULL) and $check = function_exists('getimagesize'); if ($check === FALSE) - throw new Kohana_Exception('image.getimagesize_missing'); + throw new Kohana_Exception('The Image library requires the getimagesize() PHP function, which is not available in your installation.'); // Check to make sure the image exists if ( ! is_file($image)) - throw new Kohana_Exception('image.file_not_found', $image); + throw new Kohana_Exception('The specified image, :image:, was not found. Please verify that images exist by using file_exists() before manipulating them.', array(':image:' => $image)); // Disable error reporting, to prevent PHP warnings $ER = error_reporting(0); @@ -85,11 +91,11 @@ class Image_Core { // Make sure that the image is readable and valid if ( ! is_array($image_info) OR count($image_info) < 3) - throw new Kohana_Exception('image.file_unreadable', $image); + throw new Kohana_Exception('The file specified, :file:, is not readable or is not an image', array(':file:' => $image)); // Check to make sure the image type is allowed if ( ! isset(Image::$allowed_types[$image_info[2]])) - throw new Kohana_Exception('image.type_not_allowed', $image); + throw new Kohana_Exception('The specified image, :type:, is not an allowed image type.', array(':type:' => $image)); // Image has been validated, load it $this->image = array @@ -102,6 +108,8 @@ class Image_Core { 'mime' => $image_info['mime'] ); + $this->determine_orientation(); + // Load configuration $this->config = (array) $config + Kohana::config('image'); @@ -110,14 +118,40 @@ class Image_Core { // Load the driver if ( ! Kohana::auto_load($driver)) - throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this)); + throw new Kohana_Exception('The :driver: driver for the :library: library could not be found', + array(':driver:' => $this->config['driver'], ':library:' => get_class($this))); // Initialize the driver $this->driver = new $driver($this->config['params']); // Validate the driver if ( ! ($this->driver instanceof Image_Driver)) - throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Image_Driver'); + throw new Kohana_Exception('The :driver: driver for the :library: library must implement the :interface: interface', + array(':driver:' => $this->config['driver'], ':library:' => get_class($this), ':interface:' => 'Image_Driver')); + } + + /** + * Works out the correct orientation for the image + * + * @return void + */ + protected function determine_orientation() + { + switch (TRUE) + { + case $this->image['height'] > $this->image['width']: + $orientation = Image::PORTRAIT; + break; + + case $this->image['height'] < $this->image['width']: + $orientation = Image::LANDSCAPE; + break; + + default: + $orientation = Image::SQUARE; + } + + $this->image['orientation'] = $orientation; } /** @@ -134,7 +168,8 @@ class Image_Core { } else { - throw new Kohana_Exception('core.invalid_property', $property, get_class($this)); + throw new Kohana_Exception('The :property: property does not exist in the :class: class.', + array(':property:' => $property, ':class:' => get_class($this))); } } @@ -153,13 +188,13 @@ class Image_Core { public function resize($width, $height, $master = NULL) { if ( ! $this->valid_size('width', $width)) - throw new Kohana_Exception('image.invalid_width', $width); + throw new Kohana_Exception('The width you specified, :width:, is not valid.', array(':width:' => $width)); if ( ! $this->valid_size('height', $height)) - throw new Kohana_Exception('image.invalid_height', $height); + throw new Kohana_Exception('The height you specified, :height:, is not valid.', array(':height:' => $height)); if (empty($width) AND empty($height)) - throw new Kohana_Exception('image.invalid_dimensions', __FUNCTION__); + throw new Kohana_Exception('The dimensions specified for :function: are not valid.', array(':function:' => __FUNCTION__)); if ($master === NULL) { @@ -167,7 +202,7 @@ class Image_Core { $master = Image::AUTO; } elseif ( ! $this->valid_size('master', $master)) - throw new Kohana_Exception('image.invalid_master'); + throw new Kohana_Exception('The master dimension specified is not valid.'); $this->actions['resize'] = array ( @@ -176,6 +211,8 @@ class Image_Core { 'master' => $master, ); + $this->determine_orientation(); + return $this; } @@ -194,19 +231,19 @@ class Image_Core { public function crop($width, $height, $top = 'center', $left = 'center') { if ( ! $this->valid_size('width', $width)) - throw new Kohana_Exception('image.invalid_width', $width); + throw new Kohana_Exception('The width you specified, :width:, is not valid.', array(':width:' => $width)); if ( ! $this->valid_size('height', $height)) - throw new Kohana_Exception('image.invalid_height', $height); + throw new Kohana_Exception('The height you specified, :height:, is not valid.', array(':height:' => $height)); if ( ! $this->valid_size('top', $top)) - throw new Kohana_Exception('image.invalid_top', $top); + throw new Kohana_Exception('The top offset you specified, :top:, is not valid.', array(':top:' => $top)); if ( ! $this->valid_size('left', $left)) - throw new Kohana_Exception('image.invalid_left', $left); + throw new Kohana_Exception('The left offset you specified, :left:, is not valid.', array(':left:' => $left)); if (empty($width) AND empty($height)) - throw new Kohana_Exception('image.invalid_dimensions', __FUNCTION__); + throw new Kohana_Exception('The dimensions specified for :function: are not valid.', array(':function:' => __FUNCTION__)); $this->actions['crop'] = array ( @@ -216,6 +253,8 @@ class Image_Core { 'left' => $left, ); + $this->determine_orientation(); + return $this; } @@ -269,7 +308,7 @@ class Image_Core { // Check to make sure the image type is allowed if ( ! isset(Image::$allowed_types[$image_info[2]])) - throw new Kohana_Exception('image.type_not_allowed', $overlay_file); + throw new Kohana_Exception('The specified image, :type:, is not an allowed image type.', array(':type:' => $overlay_file)); $this->actions['composite'] = array ( @@ -293,7 +332,7 @@ class Image_Core { public function flip($direction) { if ($direction !== Image::HORIZONTAL AND $direction !== Image::VERTICAL) - throw new Kohana_Exception('image.invalid_flip'); + throw new Kohana_Exception('The flip direction specified is not valid.'); $this->actions['flip'] = $direction; @@ -335,7 +374,7 @@ class Image_Core { * @param boolean keep or discard image process actions * @return object */ - public function save($new_image = FALSE, $chmod = 0644, $keep_actions = FALSE) + public function save($new_image = FALSE, $chmod = 0644, $keep_actions = FALSE, $background = NULL) { // If no new image is defined, use the current image empty($new_image) and $new_image = $this->image['file']; @@ -348,9 +387,9 @@ class Image_Core { $dir = str_replace('\\', '/', realpath($dir)).'/'; if ( ! is_writable($dir)) - throw new Kohana_Exception('image.directory_unwritable', $dir); + throw new Kohana_Exception('The specified directory, :dir:, is not writable.', array(':dir:' => $dir)); - if ($status = $this->driver->process($this->image, $this->actions, $dir, $file)) + if ($status = $this->driver->process($this->image, $this->actions, $dir, $file, FALSE, $background)) { if ($chmod !== FALSE) { @@ -359,9 +398,11 @@ class Image_Core { } } - // Reset actions. Subsequent save() or render() will not apply previous actions. - if ($keep_actions === FALSE) + if ($keep_actions !== TRUE) + { + // Reset actions. Subsequent save() or render() will not apply previous actions. $this->actions = array(); + } return $status; } @@ -372,7 +413,7 @@ class Image_Core { * @param boolean keep or discard image process actions * @return object */ - public function render($keep_actions = FALSE) + public function render($keep_actions = FALSE, $background = NULL) { $new_image = $this->image['file']; @@ -384,11 +425,13 @@ class Image_Core { $dir = str_replace('\\', '/', realpath($dir)).'/'; // Process the image with the driver - $status = $this->driver->process($this->image, $this->actions, $dir, $file, $render = TRUE); + $status = $this->driver->process($this->image, $this->actions, $dir, $file, TRUE, $background); - // Reset actions. Subsequent save() or render() will not apply previous actions. - if ($keep_actions === FALSE) + if ($keep_actions !== TRUE) + { + // Reset actions. Subsequent save() or render() will not apply previous actions. $this->actions = array(); + } return $status; } |