midnight/image
A small image editing library for PHP, (*1)
Installation
The library is available as a Composer package:, (*2)
./composer.phar require midnight/image
Usage
Open an image:, (*3)
$image = \Midnight\Image\Image::open('path/to/image.jpg');
Apply a filter:, (*4)
$image->fit(array('width' => 150, 'height' => 200));
Get the new image after applying a filter:, (*5)
$image->getFile();
Included filters
The library comes with some pre-defined filters. They can be accessed by calling the filter name as a method of the
image object. All filters accept an options array as their only argument. For example:, (*6)
$options = array('foo' => true, 'bar' => 42);
$filtered_image = $image->filterName($options);
fit
Scales the image up or down to fit it inside a rectangle defined by the options width
and height
. It's the equivalent of CSS's background-size: contain
property or Adobe InDesign's "Fit proportionally" option., (*7)
// $image has a size of 1500 by 2000 pixels
$thumbnail = $image->fit(array('width' => 200, 'height' => 200));
// $thumbnail is 150 x 200 pixels
fill
The fill
filter fills a rectangle defined by the options width
and height
. It's the equivalent of CSS's background-size: cover
property or Adobe InDesign's "Fill proportionally" option. It will most likely crop parts of the image., (*8)
// $image has a size of 1500 by 2000 pixels
$thumbnail = $image->fill(array('width' => 200, 'height' => 200));
// $thumbnail is 200 x 200 pixels, with cropped areas at the top and bottom
brighten
Brightens the image. The only option is amount
, which accepts an integer from -255 (which actually darkens the image) to 255., (*9)
$brightened = $image->brighten(array('amount' => 50));
desaturate
Desaturates the image. The only option is amount
, which accepts an integer from -100 (which actually saturates image) to 100., (*10)
$desaturated = $image->desaturate(array('amount' => 25));
colorcast
Applies a color cast to the image. The available options are color
, which accepts an instance of Midnight\Color\Color
(actually ColorInterface
) and amount
, which accepts a value between 0 and 1., (*11)
// Apply a red color cast.
$red = new \Midnight\Color\Color(255, 0, 0);
$colorcasted = $image->colorcast(array('color' => $red, 'amount' => .2));
noise
Adds noise to the image. The only option available is amount
., (*12)
$noisy = $image->noise(array('amount' => .1));
Adding a custom filter
You can easily add a custom filter:, (*13)
class MyCustomFilter extends \Midnight\Image\Filter\AbstractGdFilter
{
public function filter($image)
{
parent::filter($image);
$cache = $this->getCache();
if ($cache->exists($this)) {
return Image::open($cache->getPath($this));
}
$im = $this->getGdImage();
// Do stuff to GD image resource $im
$image = $this->save($im);
return $image;
}
}
$plugin_manager = \Midnight\Image\ImagePluginManager::getInstance();
$plugin_manager->setService('myCustomFilter', 'MyCustomFilter');
// Use the filter
$filtered = \Midnight\Image\Image::open('file.jpg')->myCustomFilter();