Novis\Image\Image::open
Novis\Image\Image::open(
Core\Bytes::Source $data,
?{ autoOrient?: bool, toSrgb?: bool, maxPixels?: uint, page?: uint, dpi?: float } $options = null,
): ImageStarts an image from an encoded file, such as a JPEG, PNG, WebP, GIF, AVIF or JPEG XL upload.
You can pass bytes, an open Core\IO\File or an uploaded Core\Request\Part. Image::open does not read the data yet. The data is read and decoded each time encode, encodeVariants or raw runs, so call them while the file is still open. If the data is not an image, encode, encodeVariants or raw throws an error.
By default, the image is turned upright. A camera writes an orientation into the file when a photo is taken sideways, and open applies it. The colours are also converted to sRGB, the colour space that screens and browsers expect. Both steps work the same for every format, AVIF and JPEG XL included. Pass autoOrient: false or toSrgb: false to switch either one off.
maxPixels sets the largest number of pixels the image may have, counted as width times height. A larger image throws an error before any pixel is decoded. Without it, the server's own limit applies.
Good to know: to read the size and the format without decoding the pixels, use Image::info.
Parameters
$dataCore\Bytes::Source$options?{ autoOrient?: bool, toSrgb?: bool, maxPixels?: uint, page?: uint, dpi?: float }default null
Return value
Returns Image.
Examples
Read an upload
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to open the bytes of an uploaded file and read the size of the image.use Novis\Image\Image;
// These PNG bytes replace a file that a user uploaded.tainted bytes $upload = Image::create(300, 200, Color::hex('#336699'))->withFormat(Format::Png)->encode();
// `open` keeps the bytes. `raw` decodes them. This prints 300x200.var $pixels = Image::open($upload)->raw();echo $pixels->width, 'x', $pixels->height, "\n";300x200Turn a photo upright
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how `open` turns a photo upright when the camera held it sideways.use Novis\Image\Image;
// A 400 by 200 JPEG replaces a photo from a camera.tainted bytes $jpeg = Image::create(400, 200, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();
// These bytes are an EXIF block with orientation 6. A camera writes this when it was turned sideways.bytes $exif = Core\Encoding::fromHex('ffe1002245786966000049492a0008000000010012010300010000000600000000000000');// The EXIF block goes right after the first two bytes of the JPEG.array<tainted bytes> $parts = [Core\Bytes::slice($jpeg, 0, 2), $exif, Core\Bytes::slice($jpeg, 2)];tainted bytes $photo = Core\Bytes::join($parts);
// By default, the image is turned upright. This prints 200x400.var $upright = Image::open($photo)->raw();echo $upright->width, 'x', $upright->height, "\n";
// With `autoOrient: false`, the pixels stay as they are stored. This prints 400x200.var $stored = Image::open($photo, { autoOrient: false })->raw();echo $stored->width, 'x', $stored->height, "\n";200x400400x200Limit the pixels
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to stop an upload that has more pixels than your page needs.use Novis\Image\Image;
// A 3000 by 2000 PNG has 6 million pixels.tainted bytes $upload = Image::create(3000, 2000, Color::hex('#336699'))->withFormat(Format::Png)->encode();
// `maxPixels` allows at most 2 million pixels. `raw` throws an error before it decodes a pixel.try { Image::open($upload, { maxPixels: 2000000 })->raw(); echo "decoded\n";} catch (Throwable $e) { echo 'too large: ', $e->message, "\n";}too large: the image is 3000x2000, which is 6000000 pixels, over the cap of 2000000 pixelsMake a jpeg from an avif
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to open an AVIF photo and save a smaller copy as a JPEG.use Novis\Image\Image;
// These AVIF bytes replace a photo that a user uploaded from a phone.tainted bytes $upload = Image::create(800, 600, Color::hex('#336699'))->withFormat(Format::Avif)->encode();
// `open` reads AVIF like every other format. This prints 200x150 and jpg.tainted bytes $copy = Image::open($upload)->resized({ width: 200 })->withFormat(Format::Jpeg)->encode();var $info = Image::info($copy);echo $info->width, 'x', $info->height, "\n";echo Image::extension($info->format), "\n";200x150jpg