mako\image\Imagick


Description


Class that manipulates images using Imagick.


Class methods


Toggle source

public __construct($file)


Constructor.


Parameters

Type Description
string Path to the image file
Return value

NULL

public function __construct($file)
{
	static $check = false;

	// Check if all the requirements are met

	if($check === false)
	{	
		if(class_exists('\Imagick', false) === false)
		{
			throw new RuntimeException(vsprintf("%s(): Imagick is not available.", array(__METHOD__)));
		}

		$check = true;
	}
	
	// Create image

	$this->image = new PHP_Imagick($file);
}

Toggle source

public __destruct()


Destructor.

Return value

NULL

public function __destruct()
{
	$this->image->destroy();
}

Toggle source

public rotate($degrees)


Rotates the image using the given angle in degrees.


Parameters

Type Description
int Degrees to rotate the image
Return value

mako\image\Imagick

public function rotate($degrees)
{
	$this->image->rotateImage(new ImagickPixel('none'), (360 - $degrees));

	return $this;
}

Toggle source

public resize($width, $height = NULL, $aspectRatio = NULL)


Resizes the image to the chosen size.


Parameters

Type Description
int Width of the image
int (optional) Height of the image
int (optional) Aspect ratio
Return value

mako\image\Imagick

public function resize($width, $height = null, $aspectRatio = null)
{
	$w = $this->image->getImageWidth();
	$h = $this->image->getImageHeight();

	if($height === null)
	{				
		$newWidth  = round($w * ($width / 100));
		$newHeight = round($h * ($width / 100));
	}
	else
	{
		if($aspectRatio === Image::AUTO)
		{
			// Calculate smallest size based on given height and width while maintaining aspect ratio

			$percentage = min(($width / $w), ($height / $h));

			$newWidth  = round($w * $percentage);
			$newHeight = round($h * $percentage);
		}
		else if($aspectRatio === Image::WIDTH)
		{
			// Base new size on given width while maintaining aspect ratio

			$newWidth  = $width;
			$newHeight = round($h * ($width / $w));
		}
		else if($aspectRatio === Image::HEIGHT)
		{
			// Base new size on given height while maintaining aspect ratio

			$newWidth  = round($w * ($height / $h));
			$newHeight = $height;
		}
		else
		{
			// Ignone aspect ratio
			
			$newWidth  = $width;
			$newHeight = $height;
		}					
	}
	
	$this->image->scaleImage($newWidth, $newHeight);
	
	return $this;
}

Toggle source

public crop($width, $height, $x, $y)


Crops the image.


Parameters

Type Description
int Width of the crop
int Height of the crop
int The X coordinate of the cropped region's top left corner
int The Y coordinate of the cropped region's top left corner
Return value

mako\image\Imagick

public function crop($width, $height, $x, $y)
{			
	$this->image->cropImage($width, $height, $x, $y);
	
	return $this;
}

Toggle source

public flip($direction = NULL)


Flips the image.


Parameters

Type Description
int (optional) Direction to flip the image
Return value

mako\image\Imagick

public function flip($direction = null)
{
	if($direction ===  Image::VERTICAL)
	{
		// Flips the image in the vertical direction

		$this->image->flipImage();
	}
	else
	{
		// Flips the image in the horizontal direction

		$this->image->flopImage();
	}

	return $this;
}

Toggle source

public watermark($file, $position = NULL, $opacity = 100)


Adds a watermark to the image.


Parameters

Type Description
string Path to the image file
int (optional) Position of the watermark
int (optional) Opacity of the watermark in percent
Return value

mako\image\Imagick

public function watermark($file, $position = null, $opacity = 100)
{
	// Check if the image exists

	if(file_exists($file) === false)
	{
		throw new RuntimeException(vsprintf("%s(): The image file ('%s') does not exist.", array(__METHOD__, $file)));
	}
	
	$watermark = new PHP_Imagick($file);
	
	$watermarkW = $watermark->getImageWidth();
	$watermarkH = $watermark->getImageHeight();
	
	// Make sure that opacity is between 0 and 100
	
	$opacity = max(min((int) $opacity, 100), 0);
	
	if($opacity < 100)
	{				
		$watermark->evaluateImage(PHP_Imagick::EVALUATE_MULTIPLY, ($opacity / 100), PHP_Imagick::CHANNEL_ALPHA);
	}
	
	// Position the watermark.
	
	switch($position)
	{
		case Image::TOP_RIGHT:
			$x = $this->image->getImageWidth() - $watermarkW;
			$y = 0;
		break;
		case Image::BOTTOM_LEFT:
			$x = 0;
			$y = $this->image->getImageHeight() - $watermarkH;
		break;
		case Image::BOTTOM_RIGHT:
			$x = $this->image->getImageWidth() - $watermarkW;
			$y = $this->image->getImageHeight() - $watermarkH;
		break;
		case Image::CENTER:
			$x = ($this->image->getImageWidth() / 2) - ($watermarkW / 2);
			$y = ($this->image->getImageHeight() / 2) - ($watermarkH / 2);
		break;
		default:
			$x = 0;
			$y = 0;
	}
	
	$this->image->compositeImage($watermark, PHP_Imagick::COMPOSITE_OVER, $x, $y);
	
	$watermark->destroy();
	
	return $this;
}

Toggle source

public greyscale()


Converts image to greyscale.

Return value

mako\image\Imagick

public function greyscale()
{
	$this->image->setImageType(PHP_Imagick::IMGTYPE_GRAYSCALE);
		
	return $this;
}

Toggle source

public colorize($color)


Colorize an image.


Parameters

Type Description
string Hex code for the color
Return value

mako\image\Imagick

public function colorize($color)
{
	$this->image->colorizeImage($color, 1.0);

	return $this;
}

Toggle source

public border($color = '#000', $thickness = 5)


Adds a border to the image.


Parameters

Type Description
string Hex code for the color
int Thickness of the frame in pixels
Return value

mako\image\Imagick

public function border($color = '#000', $thickness = 5)
{
	$this->image->shaveImage($thickness, $thickness);
	
	$this->image->borderImage($color, $thickness, $thickness);
	
	return $this;
}

Toggle source

public save($file, $quality = 85)


Saves image to file and in the specified quality (quality only affects jpg/jpeg and png).


Parameters

Type Description
string Path to the image file
int (optional) Image quality in percent
Return value

NULL

public function save($file, $quality = 85)
{
	// Check if image save path is writable

	$pathInfo = pathinfo($file);

	if(!is_writable($pathInfo['dirname']))
	{
		throw new RuntimeException(vsprintf("%s(): '%s' is not writable.", array(__METHOD__, $pathInfo['dirname'])));
	}
	
	// Make sure that quality is between 0 and 100
	
	$quality = max(min((int) $quality, 100), 0);
	
	// Save image
	
	$this->image->setImageCompression($quality);
	
	$this->image->writeImage($file);
}