Skip to content

[image]

image.max_pixels

The most pixels Novis\Image decodes in one image. You set it in nvs.toml as the key max_pixels in the block [image].

Before Novis\Image decodes an image, it reads the width and height from the file's header. If width times height is more than max_pixels, the call throws a RuntimeError, and nothing is decoded. For an animation, the pixels of every frame count. This protects the server from a decompression bomb: a small file whose header claims a huge image.

The value is a number of pixels. K means 1,024 and M means 1,048,576, so the default "24M" is 25,165,824 pixels. That is enough for a photo from a phone camera. false and 0 are not allowed.

Only the person who runs the server sets this value. A program cannot raise it. A call to Image::open may pass a lower cap with the option maxPixels, and a higher value has no effect.

The example below decodes a small upload and stops a huge one, with the cap set to "1K".

Only nvs.toml sets this key. A request may not change it.

A change in the file takes effect a few seconds after you save it. The server does not restart.

The examples below run with this configuration file.

nvs.toml
# `Novis\Image` decodes no image with more than 1024 pixels (1K).
[image]
max_pixels = "1K"

An upload too big to decode

<?nvs
// Shows how `max_pixels` stops a huge image before it is decoded.
use Novis\Image\Image;
// A PNG of 3 by 2 pixels, written as hex.
bytes $avatar = Core\Encoding::fromHex(
'89504e470d0a1a0a0000000d49484452000000030000000208060000009d74661a'
. '0000000b49444154789c6360c00500001a0001bc3ce0410000000049454e44ae426082',
);
// The header of this PNG says it is 16384 by 16384 pixels. The file is
// only 62 bytes long, but decoding it would need 1 GB of memory.
bytes $bomb = Core\Encoding::fromHex(
'89504e470d0a1a0a0000000d4948445200004000000040000806000000a9c81084'
. '000000044944415478010000456a05430000000049454e44ae426082',
);
var $uploads = ['avatar.png' => $avatar, 'bomb.png' => $bomb];
foreach ($uploads as string $name => bytes $data) {
try {
tainted bytes $png = Image::open($data)->encode();
echo $name, ": decoded\n";
} catch (RuntimeError $tooBig) {
// The cap in `nvs.toml` is 1K, so this image is refused.
echo $name, ': ', $tooBig->message, "\n";
}
}
Output
avatar.png: decoded
bomb.png: the image is 16384x16384, which is 268435456 pixels, over the cap of 1024 pixels