This post presents Imanee, an image manipulation library for PHP.

Image manipulation is a funny and quite interesting area to explore in any programming language. I’ve worked many years as an independent developer producing web apps that deal with image manipulation – morphs, photo montages and effects, creating animated gifs and such. One (very old) example is mymagz.in, where you can create a magazine cover using your photo.

With the promise of a big refactoring in one of these websites I started working on an image library to make my life easier, the easiest possible actually in terms of dealing with images in PHP. That’s when I started developing Imanee. The refactoring never happened, but I kept developing Imanee for new projects like placephant.com .

This week I released version 1.1 of Imanee – this new version brings more stability and makes room for tests and other nice improvements, including support to GD (before this release, Imanee only suported Imagemagick).

This post is a “getting started” for those who would like to give Imanee a try.

GD or Imagemagick?

Imanee works as a wrapper, currently supporting Imagemagick through the Imagick extension for PHP (php5-imagick), and GD through the GD extension (php5-gd). You’ll need one of these installed, and even though GD offer almost all the features Imagick provides, it doesn’t have support for animated gifs, so that is up to you. If you don’t need animated gifs you will be just fine with GD. On Ubuntu, installing these extensions is super easy:

$ sudo apt-get install php5-imagick

or

$ sudo apt-get install php5-gd

Restart the web server and you are golden.

Imanee will always give precedence to Imagick, since it’s the most complete in features. If it cannot find the Imagick extension, Imanee will try to use GD instead. You can also force the use of a specific extension / image resource type.

Adding Imanee to your project

Including Imanee to a project is easily done via Composer:

$ composer require imanee/imanee

Getting Started

Basic operations are pretty straighforward.

header(“content-type: image/jpeg”);
$image = new Imanee(‘myimage.jpg’);

#proportional resize
echo $image
->resize(300, 300)
->output;

This will give you the following result:

For a fixed size miniature, it’s better to use the thumbnail method with automatic crop (third argument set to true). This is how it’s done:

header(“content-type: image/jpeg”);
$image = new Imanee(‘myimage.jpg’);

#proportional resize
echo $image
->thumbnail(200, 200, true)
->output;

And this is the result:

For more info on the basic methods, check the documentation: Basic Imanee Operations

The Fun Part – Filters, Collages, Text, Animated Gifs

So far you just saw the basic, boring stuff. But Imanee was created for more exciting things – like applying filters to images, creating animated gifs, collages and writing texts, everything made super easy through a OO approach.

Filters

Imanee comes with a few built-in filters but you are encouraged to created your own filters. This is an example using the built-in black and white filter:

header(“Content-type: image/jpg”);
$imanee = new Imanee(__DIR__ . ‘/../resources/img01.jpg’);

echo $imanee->applyFilter(‘filter_bw’)
->output();

More detailed information, including how to create your own filters, can be found in the documentation: Using Filters with Imanee

Collages

The big plus on Imanee is the relative positioning that makes it a piece of cake to add collages to an image. The example below will add a png on each corner of the image, using only placement constants – no need to deal with annoying coordinates.

use Imanee\Imanee;

$res_jpg = __DIR__ . ‘/../resources/img01.jpg’;

$res_png1 = __DIR__ . ‘/../resources/cat01.png’;
$res_png2 = __DIR__ . ‘/../resources/cat02.png’;
$res_png3 = __DIR__ . ‘/../resources/cat03.png’;
$res_png4 = __DIR__ . ‘/../resources/cat04.png’;

header(“Content-type: image/jpg”);

$imanee = new Imanee($res_jpg);

$imanee
->placeImage($res_png1, Imanee::IM_POS_TOP_LEFT)
->placeImage($res_png2, Imanee::IM_POS_TOP_RIGHT)
->placeImage($res_png3, Imanee::IM_POS_BOTTOM_LEFT)
->placeImage($res_png4, Imanee::IM_POS_BOTTOM_RIGHT);

echo $imanee->output();

This is the result:

It is also possible to use absolute positioning. For more details on collages, check the documentation: Creating Collages with Imanee

Text Writing

The same approach of relative positioning can be used to write text to images. Beyond that, it’s also possible to make a text occupy a certain width, without the need to set a fixed font size. Below you can find the code I use to create placeholder images on Placephant:

$imanee = new Imanee(__DIR__ . ‘/resources/img01.jpg’);

// these values should be dynamic
$width = 300;
$height = 250;

$imanee
->applyFilter(‘filter_bw’)
->thumbnail($width, $height, true)
->placeText($width . ‘x’ . $height, Imanee::IM_POS_MID_CENTER, $imanee->getWidth() * 0.9); // 90% of img width for padding

echo $imanee->output();

This will generate the following output:

For more details on text writing, check the documentation: Writing Text with Imanee

Animated Gifs

Creating animated gifs is only possible when using the Imagick extension, because the GD library doesn’t have support for that. The following example shows how to create a “rainbow” text gif:

header(“Content-type: image/gif”);

$text = “Imanee!”;
$font = __DIR__ . ‘/resources/almonte_wood.ttf’;
$colors = [‘green’, ‘red’, ‘yellow’, ‘blue’];

$base = new Imanee();
$drawer = new Drawer();

$drawer->setFont($font)
->setFontSize(80);

foreach ($colors as $color) {
$drawer->setFontColor($color);
$frame = Imanee::textGen($text, $drawer);
$base->addFrame($frame);
}

echo $base->animate();

The result:

For more details on animated gifs, check the documentation: Creating Animated Gifs with Imanee

Wrapping Up

Imanee is meant to be the easiest way to deal with images in PHP. Obviously, we are still on early stages and some things might not work as expected, but this is an open source project and any help is appreciated. Check out our Github repository: https://github.com/imanee/imanee

If you want to have a look at more demos, check our live demos website – http://imanee.io .