abstract Image
Kohana_Image

Class Contents

Class declared in MODPATH/image/classes/image.php on line 3.

Constants

NONE
integer 1
WIDTH
integer 2
HEIGHT
integer 3
AUTO
integer 4
INVERSE
integer 5
HORIZONTAL
integer 17
VERTICAL
integer 18

Properties

public static $default_driver
string(2) "GD"
public $file
public $height
public $type
public $width

Methods

public __construct( )
Kohana_Image

Source Code
public function __construct($file)
{
	try
	{
		// Get the real path to the file
		$file = realpath($file);

		// Get the image information
		$info = getimagesize($file);
	}
	catch (Exception $e)
	{
		// Ignore all errors while reading the image
	}

	if (empty($file) OR empty($info))
	{
		throw new Kohana_Exception('Not an image or invalid image: :file',
			array(':file' => Kohana::debug_path($file)));
	}

	// Store the image information
	$this->file   = $file;
	$this->width  = $info[0];
	$this->height = $info[1];
	$this->type   = $info[2];
	$this->mime   = image_type_to_mime_type($this->type);
}

public __toString( )
Kohana_Image

Source Code
public function __toString()
{
	try
	{
		// Render the current image
		return $this->render();
	}
	catch (Exception $e)
	{
		if (is_object(Kohana::$log))
		{
			// Get the text of the exception
			$error = Kohana::exception_text($e);

			// Add this exception to the log
			Kohana::$log->add(Kohana::ERROR, $error);
		}

		// Showing any kind of error will be "inside" image data
		return '';
	}
}

abstract protected _do_background( )
Kohana_Image

Source Code
abstract protected function _do_background($r, $g, $b, $opacity);

abstract protected _do_crop( )
Kohana_Image

Source Code
abstract protected function _do_crop($width, $height, $offset_x, $offset_y);

abstract protected _do_flip( )
Kohana_Image

Source Code
abstract protected function _do_flip($direction);

abstract protected _do_reflection( )
Kohana_Image

Source Code
abstract protected function _do_reflection($height, $opacity, $fade_in);

abstract protected _do_render( )
Kohana_Image

Source Code
abstract protected function _do_render($type, $quality);

abstract protected _do_resize( )
Kohana_Image

Source Code
abstract protected function _do_resize($width, $height);

abstract protected _do_rotate( )
Kohana_Image

Source Code
abstract protected function _do_rotate($degrees);

abstract protected _do_save( )
Kohana_Image

Source Code
abstract protected function _do_save($file, $quality);

abstract protected _do_sharpen( )
Kohana_Image

Source Code
abstract protected function _do_sharpen($amount);

abstract protected _do_watermark( )
Kohana_Image

Source Code
abstract protected function _do_watermark(Image $image, $offset_x, $offset_y, $opacity);

public background( )
Kohana_Image

Source Code
public function background($color, $opacity = 100)
{
	if ($color[0] === '#')
	{
		// Remove the pound
		$color = substr($color, 1);
	}

	if (strlen($color) === 3)
	{
		// Convert shorthand into longhand hex notation
		$color = preg_replace('/./', '$0$0', $color);
	}

	// Convert the hex into RGB values
	list ($r, $g, $b) = array_map('hexdec', str_split($color, 2));

	// The opacity must be in the range of 0 to 100
	$opacity = min(max($opacity, 0), 100);

	$this->_do_background($r, $g, $b, $opacity);

	return $this;
}

public crop( )
Kohana_Image

Source Code
public function crop($width, $height, $offset_x = NULL, $offset_y = NULL)
{
	if ($width > $this->width)
	{
		// Use the current width
		$width = $this->width;
	}

	if ($height > $this->height)
	{
		// Use the current height
		$height = $this->height;
	}

	if ($offset_x === NULL)
	{
		// Center the X offset
		$offset_x = round(($this->width - $width) / 2);
	}
	elseif ($offset_x === TRUE)
	{
		// Bottom the X offset
		$offset_x = $this->width - $width;
	}
	elseif ($offset_x < 0)
	{
		// Set the X offset from the right
		$offset_x = $this->width - $width + $offset_x;
	}

	if ($offset_y === NULL)
	{
		// Center the Y offset
		$offset_y = round(($this->height - $height) / 2);
	}
	elseif ($offset_y === TRUE)
	{
		// Bottom the Y offset
		$offset_y = $this->height - $height;
	}
	elseif ($offset_y < 0)
	{
		// Set the Y offset from the bottom
		$offset_y = $this->height - $height + $offset_y;
	}

	// Determine the maximum possible width and height
	$max_width  = $this->width  - $offset_x;
	$max_height = $this->height - $offset_y;

	if ($width > $max_width)
	{
		// Use the maximum available width
		$width = $max_width;
	}

	if ($height > $max_height)
	{
		// Use the maximum available height
		$height = $max_height;
	}

	$this->_do_crop($width, $height, $offset_x, $offset_y);

	return $this;
}

public static factory( )
Kohana_Image

Source Code
public static function factory($file, $driver = NULL)
{
	if ($driver === NULL)
	{
		// Use the default driver
		$driver = Image::$default_driver;
	}

	// Set the class name
	$class = 'Image_'.$driver;

	return new $class($file);
}

public flip( )
Kohana_Image

Source Code
public function flip($direction)
{
	if ($direction !== Image::HORIZONTAL)
	{
		// Flip vertically
		$direction = Image::VERTICAL;
	}

	$this->_do_flip($direction);

	return $this;
}

public reflection( )
Kohana_Image

Source Code
public function reflection($height = NULL, $opacity = 100, $fade_in = FALSE)
{
	if ($height === NULL OR $height > $this->height)
	{
		// Use the current height
		$height = $this->height;
	}

	// The opacity must be in the range of 0 to 100
	$opacity = min(max($opacity, 0), 100);

	$this->_do_reflection($height, $opacity, $fade_in);

	return $this;
}

public render( )
Kohana_Image

Source Code
public function render($type = NULL, $quality = 100)
{
	if ($type === NULL)
	{
		// Use the current image type
		$type = image_type_to_extension($this->type, FALSE);
	}

	return $this->_do_render($type, $quality);
}

public resize( )
Kohana_Image

Source Code
public function resize($width = NULL, $height = NULL, $master = NULL)
{
	if ($master === NULL)
	{
		// Choose the master dimension automatically
		$master = Image::AUTO;
	}
	// Image::WIDTH and Image::HEIGHT depricated. You can use it in old projects,
	// but in new you must pass empty value for non-master dimension
	elseif ($master == Image::WIDTH AND ! empty($width))
	{
		$master = Image::AUTO;

		// Set empty height for backvard compatibility
		$height = NULL;
	}
	elseif ($master == Image::HEIGHT AND ! empty($height))
	{
		$master = Image::AUTO;

		// Set empty width for backvard compatibility
		$width = NULL;
	}

	if (empty($width))
	{
		if ($master === Image::NONE)
		{
			// Use the current width
			$width = $this->width;
		}
		else
		{
			// If width not set, master will be height
			$master = Image::HEIGHT;
		}
	}

	if (empty($height))
	{
		if ($master === Image::NONE)
		{
			// Use the current height
			$height = $this->height;
		}
		else
		{
			// If height not set, master will be width
			$master = Image::WIDTH;
		}
	}

	switch ($master)
	{
		case Image::AUTO:
			// Choose direction with the greatest reduction ratio
			$master = ($this->width / $width) > ($this->height / $height) ? Image::WIDTH : Image::HEIGHT;
		break;
		case Image::INVERSE:
			// Choose direction with the minimum reduction ratio
			$master = ($this->width / $width) > ($this->height / $height) ? Image::HEIGHT : Image::WIDTH;
		break;
	}

	switch ($master)
	{
		case Image::WIDTH:
			// Recalculate the height based on the width proportions
			$height = $this->height * $width / $this->width;
		break;
		case Image::HEIGHT:
			// Recalculate the width based on the height proportions
			$width = $this->width * $height / $this->height;
		break;
	}

	// Convert the width and height to integers, minimum value is 1px
	$width  = max(round($width), 1);
	$height = max(round($height), 1);

	$this->_do_resize($width, $height);

	return $this;
}

public rotate( )
Kohana_Image

Source Code
public function rotate($degrees)
{
	// Make the degrees an integer
	$degrees = (int) $degrees;

	if ($degrees > 180)
	{
		do
		{
			// Keep subtracting full circles until the degrees have normalized
			$degrees -= 360;
		}
		while($degrees > 180);
	}

	if ($degrees < -180)
	{
		do
		{
			// Keep adding full circles until the degrees have normalized
			$degrees += 360;
		}
		while($degrees < -180);
	}

	$this->_do_rotate($degrees);

	return $this;
}

public save( )
Kohana_Image

Source Code
public function save($file = NULL, $quality = 100)
{
	if ($file === NULL)
	{
		// Overwrite the file
		$file = $this->file;
	}

	if (is_file($file))
	{
		if ( ! is_writable($file))
		{
			throw new Kohana_Exception('File must be writable: :file',
				array(':file' => Kohana::debug_path($file)));
		}
	}
	else
	{
		// Get the directory of the file
		$directory = realpath(pathinfo($file, PATHINFO_DIRNAME));

		if ( ! is_dir($directory) OR ! is_writable($directory))
		{
			throw new Kohana_Exception('Directory must be writable: :directory',
				array(':directory' => Kohana::debug_path($directory)));
		}
	}

	// The quality must be in the range of 1 to 100
	$quality = min(max($quality, 1), 100);

	return $this->_do_save($file, $quality);
}

public sharpen( )
Kohana_Image

Source Code
public function sharpen($amount)
{
	// The amount must be in the range of 1 to 100
	$amount = min(max($amount, 1), 100);

	$this->_do_sharpen($amount);

	return $this;
}

public watermark( )
Kohana_Image

Source Code
public function watermark(Image $watermark, $offset_x = NULL, $offset_y = NULL, $opacity = 100)
{
	if ($offset_x === NULL)
	{
		// Center the X offset
		$offset_x = round(($this->width - $watermark->width) / 2);
	}
	elseif ($offset_x === TRUE)
	{
		// Bottom the X offset
		$offset_x = $this->width - $watermark->width;
	}
	elseif ($offset_x < 0)
	{
		// Set the X offset from the right
		$offset_x = $this->width - $watermark->width + $offset_x;
	}

	if ($offset_y === NULL)
	{
		// Center the Y offset
		$offset_y = round(($this->height - $watermark->height) / 2);
	}
	elseif ($offset_y === TRUE)
	{
		// Bottom the Y offset
		$offset_y = $this->height - $watermark->height;
	}
	elseif ($offset_y < 0)
	{
		// Set the Y offset from the bottom
		$offset_y = $this->height - $watermark->height + $offset_y;
	}

	// The opacity must be in the range of 1 to 100
	$opacity = min(max($opacity, 1), 100);

	$this->_do_watermark($watermark, $offset_x, $offset_y, $opacity);

	return $this;
}