mako\image\ImageMagick


Description


Class that manipulates images using ImageMagick.


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(function_exists('exec') === false)
		{
			throw new RuntimeException(vsprintf("%s(): The 'exec' function has been disabled.", array(__METHOD__)));
		}

		$check = true;
	}
	
	$this->image = escapeshellarg($file);
}

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\ImageMagick

public function rotate($degrees)
{		
	$this->cmd .= '-rotate ' . escapeshellarg((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\ImageMagick

public function resize($width, $height = null, $aspectRatio = null)
{
	if($height === null)
	{				
		$this->cmd .= '-resize ' . escapeshellarg((int) $width . '%') . ' ';
	}
	else
	{
		if($aspectRatio === Image::AUTO)
		{
			// Calculate smallest size based on given height and width while maintaining aspect ratio

			$this->cmd .= '-resize ' . escapeshellarg((int) $width . 'x' . (int) $height) . ' ';
		}
		else if($aspectRatio === Image::WIDTH)
		{
			// Base new size on given width while maintaining aspect ratio

			$this->cmd .= '-resize ' . escapeshellarg((int) $width) . ' ';
		}
		else if($aspectRatio === Image::HEIGHT)
		{
			// Base new size on given height while maintaining aspect ratio

			$this->cmd .= '-resize ' . escapeshellarg('x' . (int) $height) . ' ';
		}
		else
		{
			// Ignone aspect ratio
			
			$this->cmd .= '-resize ' . escapeshellarg((int) $width . 'x' . (int) $height . '!') . ' ';
		}						
	}

	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\ImageMagick

public function crop($width, $height, $x, $y)
{			
	$this->cmd .= '-crop ' . escapeshellarg((int) $width . 'x' . (int) $height . '+' . (int) $x . '+' . (int) $y) . ' ';
	
	return $this;
}

Toggle source

public flip($direction = NULL)


Flips the image.


Parameters

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

mako\image\ImageMagick

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

		$this->cmd .= '-flip ';
	}
	else
	{
		// Flips the image in the horizontal direction

		$this->cmd .= '-flop ';
	}

	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\ImageMagick

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)));
	}
	
	// Make sure that opacity is between 0 and 100
	
	$opacity = max(min((int) $opacity, 100), 0);
	
	// Position the watermark.
	
	switch($position)
	{
		case Image::TOP_RIGHT:
			$pos = 'NorthEast';
		break;
		case Image::BOTTOM_LEFT:
			$pos = 'SouthWest';
		break;
		case Image::BOTTOM_RIGHT:
			$pos = 'SouthEast';
		break;
		case Image::CENTER:
			$pos = 'Center';
		break;
		default:
			$pos = 'NorthWest';
	}
	
	$this->cmd .= '- | composite -dissolve ' . escapeshellarg($opacity) . '% -gravity ' . escapeshellarg($pos) . ' ' . escapeshellarg($file) . ' - - | convert - ';
	
	return $this;
}

Toggle source

public greyscale()


Converts image to greyscale.

Return value

mako\image\ImageMagick

public function greyscale()
{
	$this->cmd .= '-fx \'(r+g+b)/3\' ';
	
	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->cmd .= '-fill ' . escapeshellarg($color) . ' -colorize 50% ';

	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\ImageMagick

public function border($color = '#000', $thickness = 5)
{
	$this->cmd .= '-shave ' . escapeshellarg($thickness . 'x' . $thickness) . ' -bordercolor ' . escapeshellarg($color) . ' -border ' . escapeshellarg($thickness) . ' ';
	
	return $this;
}

Toggle source

public save($file, $quality = 85)


Saves image to file and in the specified quality.


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);

	// Manipulate and save image

	$file      = escapeshellarg($file);
	$quality   = escapeshellarg((int) $quality.'%');
	$this->cmd = trim($this->cmd);

	exec("convert {$this->image} {$this->cmd} -quality {$quality} {$file}", $output, $code);

	if($code !== 0)
	{
		$error = ($code === 127) ? 'ImageMagick could not be found.' : 'An error occured.';

		throw new RuntimeException(vsprintf("%s(): %s", array(__METHOD__, $error)));
	}
}