Skip to content

Novis\Image\Image::withFormat

$image->withFormat(
    Format $format,
    ?{ quality?: uint, progressive?: bool, lossless?: bool, effort?: uint } $options = null,
): Image

Sets the file format that encode and encodeVariants write.

The first argument is a Format: Format::Jpeg, Format::Png, Format::Webp, Format::Gif or Format::Avif. Novis can read Format::Jxl, Format::Svg and Format::Pdf files, but it cannot write them. Choosing one of those throws an error when encode runs.

The second argument is optional, with these keys:

  • quality is from 1 to 100, for JPEG, WebP and AVIF. A higher value gives a better picture and a larger file. A value below 1 counts as 1, and a value above 100 counts as 100.
  • lossless writes a WebP that keeps every pixel exactly. Then quality is not used.
  • progressive and effort are accepted. They do not change the file.

A JPEG has no transparent pixels, so the alpha of each pixel is removed. A GIF has at most 256 colours.

Without withFormat, encode writes the format of the file you opened. A canvas from create and pixels from fromRaw are written as PNG.

withFormat returns a new Image and does not change the one you called it on.

Parameters

  • $formatFormat
  • $options?{ quality?: uint, progressive?: bool, lossless?: bool, effort?: uint }default null

Return value

Returns Image.

Changelog

VersionChange
0.0.6Each Novis\Image\Image method that returns a new image is named for it, for example resized(), blurred() and withText(). keepingMetadata() replaces metadata({ keep: true }), and encodeVariants() replaces variants(). Image::compare returns a Comparison with the property differenceImage, which was Diff with diff.

Examples

Png to jpeg

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows how to turn a PNG upload into a JPEG file with a chosen quality.
use Novis\Image\Image;
// These PNG bytes replace a file that a user uploaded.
tainted bytes $upload = Image::create(200, 100, Color::hex('#336699'))->withFormat(Format::Png)->encode();
// `withFormat` chooses JPEG with quality 80. This prints image/jpeg and jpg.
tainted bytes $jpeg = Image::open($upload)->withFormat(Format::Jpeg, { quality: 80 })->encode();
var $info = Image::info($jpeg);
echo Image::mime($info->format), "\n";
echo Image::extension($info->format), "\n";
Output
image/jpeg
jpg

Lossless webp

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows that a lossless WebP file keeps the exact colour of every pixel.
use Novis\Image\Image;
Image $logo = Image::create(40, 40, Color::hex('#336699'));
tainted bytes $webp = $logo->withFormat(Format::Webp, { lossless: true })->encode();
// The first pixel has the same colour after the file is decoded. This prints 51 102 153.
var $pixels = Image::open($webp)->raw();
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($pixels->pixels, 0, 4), 'C4');
echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], "\n";
Output
51 102 153

Keep the input format

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows that without `withFormat`, the output has the format of the file you opened.
use Novis\Image\Image;
// These JPEG bytes replace a photo that a user uploaded.
tainted bytes $upload = Image::create(400, 300, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();
// The pipeline makes a smaller copy and does not call `withFormat`. This prints jpg 200x150.
tainted bytes $small = Image::open($upload)->resized({ width: 200 })->encode();
var $info = Image::info($small);
echo Image::extension($info->format), ' ', $info->width, 'x', $info->height, "\n";
Output
jpg 200x150