Skip to content

Resizing

Jan Hermann edited this page Oct 9, 2026 · 1 revision

📐

Resize modes and the class which resizes an Imagine image.

Enum of resize modes. Backing values match the former Nette\Utils\Image constants, so the bitmask in names of generated thumbnails stays the same.

Case Value Config name Behaviour
FIT 0 fit Fits the image into the box, keeping the aspect ratio.
SHRINK_ONLY 1 shrink_only Never enlarges the image.
STRETCH 2 stretch Ignores the aspect ratio.
FILL 4 fill Fills the whole box, keeping the aspect ratio. The overflow is kept.
EXACT 8 exact Fills the whole box, keeping the aspect ratio, and crops the overflow.

fromConfig

ResizeFlag::fromConfig(string|int|self $value): self creates a flag from shrink_only, ShrinkOnly, a class constant like Image::SHRINK_ONLY or an int. Throws \InvalidArgumentException for an unknown flag.

fromConfigList

ResizeFlag::fromConfigList(array $flags): array converts a list of config values to a list of flags.

toMask

ResizeFlag::toMask(iterable $flags): int combines flags to a bitmask, used in file names of thumbnails.

Final class with the static method Resizer::resize(ImageInterface $image, ?int $width, ?int $height, array $flags = []): ImageInterface. It resizes the image in place and returns it.

How it works

  • Without $width and $height the image is returned unchanged.
  • With only one of them, the other is calculated from the aspect ratio.
  • STRETCH with both sizes scales each axis separately.
  • Otherwise the smaller scale is used (FIT), or the larger when FILL or EXACT is set.
  • SHRINK_ONLY limits the scale to 1.
  • EXACT with both sizes crops the centre of the result to the box.
use Imagony\ImagineFactory;
use Imagony\Resizing\{ResizeFlag, Resizer};

$image = ImagineFactory::create()->open("photo.jpg");
Resizer::resize($image, 400, 400, [ResizeFlag::EXACT]);

Clone this wiki locally