Skip to content

Novis\Image\Format

An image file format.

Image::withFormat returns a copy of the image with this format set. encode runs every step you added to the image and returns the finished file in that format. Codec::info gives the format of a file that you read. The cases are Jpeg, Png, Webp, Gif, Avif, Jxl, Svg and Pdf. Avif is the AVIF format, and Jxl is the JPEG XL format.

Image::mime($format) returns the MIME type of a format, such as "image/webp". Send it in the Content-Type header. Image::extension($format) returns the file extension without the dot, such as "webp".

Good to know: Image::open reads all eight formats. encode writes only Jpeg, Png, Webp, Gif and Avif. encode throws an error for Jxl, Svg and Pdf.

Examples

Mime type and file name

<?nvs
// Shows how to get the MIME type and the file name for an image you write.
use Novis\Image\Color;
use Novis\Image\Format;
use Novis\Image\Image;
array<Format> $formats = [Format::Png, Format::Webp, Format::Jpeg];
// Each line prints the MIME type and a file name.
// For example, the first line is "image/png avatar.png".
foreach ($formats as Format $format) {
echo Image::mime($format), ' avatar.', Image::extension($format), "\n";
}
// `encode` writes the format that `withFormat` sets.
// Bytes 1 to 3 of a PNG file are the letters "PNG", so this prints PNG.
tainted bytes $png = Image::create(8, 8, Color::hex('#fff'))->withFormat(Format::Png)->encode();
echo Core\Bytes::slice($png, 1, 3) as string, "\n";
Output
image/png avatar.png
image/webp avatar.webp
image/jpeg avatar.jpg
PNG