Novis\Image\Image::info
Novis\Image\Image::info(Core\Bytes::Source $data): InfoNovis\Image\Image::info( Core\Bytes::Source $data, ): Info
Reads the format, the size and other facts about an encoded image, without decoding its pixels.
You can pass bytes, an open Core\IO\File or an uploaded Core\Request\Part. A file keeps its position, and saveTo still saves the whole upload afterwards. The result has these keys:
formatis theFormatof the file, for exampleFormat::Png.widthandheightare the size in pixels, as stored in the file.hasAlphaistruewhen the image can have transparent pixels.framesis the number of frames. An animated image has more than one.orientationis the EXIF orientation from 1 to 8. The value 1 means the image is stored upright.exifmaps each EXIF tag name, such asDateTime, to its value as a string. It isnullwhen the file has no EXIF.hasIccistruewhen the file has its own colour profile.
Bytes that are not an image throw an error.
Good to know: use info to reject an upload that is too large before you decode it. Image::mime and Image::extension turn the format into a MIME type and a file extension.
Parameters
$dataCore\Bytes::Source
Return value
Returns Info.
Examples
Check an upload
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to check the size of an upload before you decode it.use Novis\Image\Image;
// A 2400 by 1600 JPEG replaces a photo that a user uploaded.tainted bytes $upload = Image::create(2400, 1600, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();
// `info` reads only the start of the file. This prints "too wide: 2400".var $info = Image::info($upload);if ($info->width > 2000) { echo 'too wide: ', $info->width, "\n";} else { echo "accepted\n";}too wide: 2400Type from the file
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to find the real type of a file from its bytes and not from its name.use Novis\Image\Image;
// The file is a WebP image, even if the user named it "photo.png".tainted bytes $upload = Image::create(32, 32, Color::hex('#336699'))->withFormat(Format::Webp)->encode();
// This prints image/webp and webp.var $info = Image::info($upload);echo Image::mime($info->format), "\n";echo Image::extension($info->format), "\n";
// Every pixel of this image is opaque, so the file has no alpha. This prints false.echo $info->hasAlpha ? 'true' : 'false', "\n";image/webpwebpfalseNot an image
<?nvs// Shows the error that `Image::info` throws for bytes that are not an image.use Novis\Image\Image;
// A text file uploaded as if it were an image.bytes $upload = "name,price\nshirt,20\n" as bytes;
try { Image::info($upload); echo "an image\n";} catch (Throwable $e) { echo 'rejected: ', $e->message, "\n";}rejected: the data is not an image in a format the image component readsCount the frames
<?nvsuse Novis\Image\Format;// Shows how to count the frames of an animated GIF.use Novis\Image\Image;
// These bytes are a GIF with two frames of 1 by 1 pixel. The first frame is red, the second is blue.bytes $gif = Core\Encoding::fromHex('47494638396101000100800000ff00000000ff21f904000a0000002c000000000100010000020244010021f904000a0000002c00000000010001000002024c01003b');
// `frames` is the number of frames in the file. This prints 2.echo Image::info($gif)->frames, "\n";
// When the output is a GIF, every frame is written. This prints 2 again.tainted bytes $copy = Image::open($gif)->withFormat(Format::Gif)->encode();echo Image::info($copy)->frames, "\n";
// A PNG output has only the first frame. This prints 1.tainted bytes $still = Image::open($gif)->withFormat(Format::Png)->encode();echo Image::info($still)->frames, "\n";221