Skip to content

Novis\Image\Image::resized

$image->resized(Resize $options): Image$image->resized(
    Resize $options,
): Image

Changes the size of an image.

Give only width or only height, and the other side follows, so the image keeps its shape. Give both, and fit decides how the image fills that box. Image::Fit::Cover is the default. It fills the whole box and cuts off what does not fit. Image::Fit::Contain shows the whole image inside the box and leaves the rest transparent. Image::Fit::Inside keeps the shape and makes the image no larger than the box.

resized returns a new Image and does not change the one you called it on. The work happens when encode, encodeVariants or raw runs.

Good to know: resized never makes an image larger than it is. Set upscale to true when you want that.

Parameters

  • $optionsResize

Return value

Returns Image.

Changelog

VersionChange
0.0.6Each Novis\Image\Image method that returns a new image is named for it, for example resized(), blurred() and withText(). keepingMetadata() replaces metadata({ keep: true }), and encodeVariants() replaces variants(). Image::compare returns a Comparison with the property differenceImage, which was Diff with diff.

Examples

Thumbnail by width

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows how to make a thumbnail that is 300 pixels wide.
use Novis\Image\Image;
// A blank 1200 by 800 image replaces a photo that a user uploaded.
Image $photo = Image::create(1200, 800, Color::hex('#336699'));
// Only the width is given, so the height follows. This prints 300x200.
tainted bytes $thumbnail = $photo->resized({ width: 300 })->withFormat(Format::Jpeg, { quality: 80 })->encode();
var $info = Image::info($thumbnail);
echo $info->width, 'x', $info->height, "\n";
Output
300x200

Square avatar

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows how `fit` changes the result when a wide image goes into a square box.
use Novis\Image\Image;
Image $photo = Image::create(600, 400, Color::hex('#cc6633'));
// `Cover` and `Contain` both return 128x128. `Inside` keeps the shape and returns 128x85.
array<Image::Fit> $fits = [Image::Fit::Cover, Image::Fit::Contain, Image::Fit::Inside];
foreach ($fits as Image::Fit $fit) {
tainted bytes $avatar = $photo->resized({ width: 128, height: 128, fit: $fit })->withFormat(Format::Png)->encode();
var $info = Image::info($avatar);
string $name = match ($fit) {
Image::Fit::Cover => 'Cover',
Image::Fit::Contain => 'Contain',
default => 'Inside',
};
echo $name, ': ', $info->width, 'x', $info->height, "\n";
}
Output
Cover: 128x128
Contain: 128x128
Inside: 128x85

Small images stay small

<?nvs
use Novis\Image\Color;
// Shows that a small logo keeps its size unless you set `upscale`.
use Novis\Image\Image;
Image $logo = Image::create(200, 100, Color::hex('#22aa55'));
// The logo is smaller than 800 pixels, so this prints 200x100.
var $kept = $logo->resized({ width: 800 })->raw();
echo $kept->width, 'x', $kept->height, "\n";
// With `upscale: true`, the logo grows. This prints 800x400.
var $grown = $logo->resized({ width: 800, upscale: true })->raw();
echo $grown->width, 'x', $grown->height, "\n";
Output
200x100
800x400