Novis\Image\Image::encodeVariants
$image->encodeVariants(array<Variant> $set): array<tainted bytes>$image->encodeVariants( array<Variant> $set, ): array<tainted bytes>
Writes several files from one image, for example the sizes a web page needs, and decodes the input only once.
The argument is an array of entries. Each entry may have these keys:
widthandheightresize the image, asresizeddoes.fitsets how the image fills the size when bothwidthandheightare given.formatis the file format of this entry.qualityis the quality of this entry, from 1 to 100. It needs aformat, from the entry or from an earlierwithFormatcall. Without one,encodeVariantsthrows aLogicError.
Each file is tainted bytes, like the result of encode, so the result is array<tainted bytes>.
Every entry first runs the steps of the image, and then its own resize and format. encodeVariants returns one encoded file per entry, in the same order. An empty array returns an empty array.
This is faster than one encode per size, because the input is decoded only once.
Good to know: a common use is one call that writes every width of a responsive image.
Parameters
$setarray<Variant>
Return value
Returns array<tainted bytes>.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each 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. |
0.0.6 | Image::encode, the pixels of Image::raw, each file of Image::encodeVariants and the result of Pages::merge are tainted bytes. An image or a PDF file may be an upload. Font::fromBytes takes tainted bytes. |
Examples
Three widths
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to write three widths of one photo for a responsive web page.use Novis\Image\Image;
// These JPEG bytes replace a photo that a user uploaded.tainted bytes $upload = Image::create(800, 600, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();
// One call decodes the photo once and writes three files.array<tainted bytes> $files = Image::open($upload)->encodeVariants([{ width: 400 }, { width: 200 }, { width: 100 }]);
// This prints 400x300, 200x150 and 100x75.foreach ($files as tainted bytes $file) { var $info = Image::info($file); echo $info->width, 'x', $info->height, "\n";}400x300200x150100x75One size two formats
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to write the same image as WebP for new browsers and as JPEG for old ones.use Novis\Image\Image;
Image $photo = Image::create(640, 480, Color::hex('#336699'));
// Both entries resize to a 300 by 300 square. Each one has its own format.array<tainted bytes> $files = $photo->encodeVariants([ { width: 300, height: 300, fit: Image::Fit::Cover, format: Format::Webp, quality: 80 }, { width: 300, height: 300, fit: Image::Fit::Cover, format: Format::Jpeg, quality: 85 },]);
// This prints image/webp 300x300 and image/jpeg 300x300.foreach ($files as tainted bytes $file) { var $info = Image::info($file); echo Image::mime($info->format), ' ', $info->width, 'x', $info->height, "\n";}image/webp 300x300image/jpeg 300x300Quality needs a format
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows that an entry with a `quality` needs a `format`.use Novis\Image\Image;
Image $photo = Image::create(200, 200, Color::hex('#336699'));
// The entry has a quality and no format, so `encodeVariants` throws a `LogicError`.try { $photo->encodeVariants([{ width: 100, quality: 60 }]);} catch (LogicError $e) { echo $e->message, "\n";}
// After `withFormat`, every entry has a format. This prints 1.array<tainted bytes> $files = $photo->withFormat(Format::Jpeg)->encodeVariants([{ width: 100, quality: 60 }]);echo Core\Arr::count($files), "\n";a variant with a `quality` needs a `format`1