Nette Documentation Preview

Image Functions

The class Nette\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\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

In the mentioned examples the image is only scaled down. Possible enlargement will be allowed by using the directive Image::ENLARGE:

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

With the directive Image::STRETCH we can allow non-proportional changes in the size:

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

Both directives can be combined:

$image->resize(50, 30, Image::ENLARGE | 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:


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:


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

// send it like image/png

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