Image Manipulation

The class Nette\Utils\Image is intended for basic image manipulation. It simplifies the basic operations, like image resizing, sharpening or sending the image to the browser.

If you want to manipulate with images (like scaling or sharpening of images) in PHP you must write an unreadable and unintelligible code. And it wouldn't be Nette Framework if it didn't come with a better API :-)

We can start with loading of the image from a file:

use Nette\Utils\Image;

$image = Image::fromFile('nette.jpg');

or we can create new blank image, with size of 100×200:

$image = Image::fromBlank(100, 200);

Optionally we can set the background color (default is black):

$image = Image::fromBlank(100, 200, Image::rgb(125, 0, 0));

And we will write its size:

echo "Image size is: $image->width × $image->height";

Image resize

We will scale down the image proportionally so it won't exceed 50×30 pixels:

$image->resize(50, 30);

It's possible to set only one dimension and the second one will be calculated:

$image->resize(50, NULL); // width 50px, height auto

$image->resize(NULL, 30); // width auto, height 30px

Any dimension can be also set in percentage:

$image->resize('75%', 30); // 75% × 30px

The behavior of the resize function could be modified with following flags:

Flag Description
Image::FIT (default) resulting dimensions will be less or equal as specified
Image::FILL fills the target area and possibly extends it in one direction
Image::EXACT fills the whole area and cuts what exceeds
Image::SHRINK_ONLY just scales down (does not extend a small image)
Image::STRETCH does not keep the aspect ratio

Flags come as a third argument of the function:

$image->resize(50, 30, Image::STRETCH);

It is possible to combine all flags:

$image->resize(50, 30, Image::SHRINK_ONLY | Image::STRETCH);

If we write one or both of the dimensions as a negative number the image will be flipped (horizontally or vertically):

$flipped = $image->resize(NULL, '-100%'); // flip vertically

$flipped = $image->resize('-100%', '-100%');  // rotate by 180°

$flipped = $image->resize(-125, 50); // resize & flip horizontally

Image modification

After reducing the image we can improve it by sharpening:

$image->sharpen();

We can also trim the image by the coordinates of a rectangle:

$image->crop($left, $top, $width, $height);

The reduced image can be inserted into another one (so-called watermark).

$blank = Image::fromBlank(320, 240, Image::rgb(52, 132, 210));
$blank->place($image, 0, 0); // place into a position 0px, 0px

// coordinates can be set also in percentage
$blank->place($image, '80%', '75%', 25); // transparency is 25 %

Nette\Utils\Image simplifies calling of all graphic functions from the GD extension, for example imagefilledrectangle, but with the usage of more understandable object approach.

$white = Image::rgb(255, 255, 255);
$image->filledRectangle(0, 0, $width, $height, $white);

Functions sharpen() and rotate() require Bundled GD extension, so it is not sure they will work everywhere.

Image saving

The image could be saved into a file:

$image->save('resampled.jpg');

Voluntarily it's possible to set also the quality and format of the image (if the format isn't set it's detected from the file extension):

$image->save('resampled.jpg', 80, Image::JPEG); // JPEG, quality 80%

Alternatively the image could be saved into a variable:

$binary = (string) $image;

or you can send it directly to the browser with the setting of the according header Content-Type:

// send it like image/jpeg
$image->send();

// send it like image/png
$image->send(Image::PNG);

Because rendering of image by lnk to presenter which will call $image->send(); can be little bit slow, it is better to save changed file and then to show directly this saved file.

Such API is really pleasure to use, isn't it?