Class that manipulates images using ImageMagick.
Constructor.
| Type | Description |
|---|---|
| string | Path to the image file |
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);
}
Rotates the image using the given angle in degrees.
| Type | Description |
|---|---|
| int | Degrees to rotate the image |
mako\image\ImageMagick
public function rotate($degrees)
{
$this->cmd .= '-rotate ' . escapeshellarg((360 - $degrees)) . ' ';
return $this;
}
Resizes the image to the chosen size.
| Type | Description |
|---|---|
| int | Width of the image |
| int | (optional) Height of the image |
| int | (optional) Aspect ratio |
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;
}
Crops the image.
| 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 |
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;
}
Flips the image.
| Type | Description |
|---|---|
| string | (optional) Direction to flip the image |
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;
}
Adds a watermark to the image.
| Type | Description |
|---|---|
| string | Path to the image file |
| int | (optional) Position of the watermark |
| int | (optional) Opacity of the watermark in percent |
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;
}
Converts image to greyscale.
mako\image\ImageMagick
public function greyscale()
{
$this->cmd .= '-fx \'(r+g+b)/3\' ';
return $this;
}
Colorize an image.
| Type | Description |
|---|---|
| string | Hex code for the color |
mako\image\Imagick
public function colorize($color)
{
$this->cmd .= '-fill ' . escapeshellarg($color) . ' -colorize 50% ';
return $this;
}
Adds a border to the image.
| Type | Description |
|---|---|
| string | Hex code for the color |
| int | Thickness of the frame in pixels |
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;
}
Saves image to file and in the specified quality.
| Type | Description |
|---|---|
| string | Path to the image file |
| int | (optional) Image quality in percent |
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)));
}
}