Novis\Image\Image::encode
Runs every step of the image and returns the bytes of the finished file.
Methods such as resized, cropped and withFormat only add a step to a list. encode is the call that does the work. It decodes the input, runs the steps in the order you wrote them, and encodes the result.
The output has the format that withFormat chose. Without withFormat, it has the format of the file you opened. An image made with Image::create or Image::fromRaw is written as PNG. The output has no EXIF data (the camera and location details in a photo) unless you called keepingMetadata().
The result is tainted bytes, because the image may be an upload. Write tainted bytes on the variable that keeps it. A response body, a file and Core\Storage all take tainted bytes.
encode throws an error when the input is not an image. It also throws when a step cannot run, such as cropped with a box outside the image. An image with more pixels than the limit throws too.
Good to know: to write several sizes of one image, encodeVariants is faster than several encode calls, because it decodes the input only once.
Return value
Returns tainted bytes.
Changelog
| Version | Change |
|---|---|
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
Make a thumbnail
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to turn an upload into a small square thumbnail file.use Novis\Image\Image;
// These JPEG bytes replace a photo that a user uploaded.tainted bytes $upload = Image::create(1200, 800, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();
// Nothing runs until `encode`. Then the photo is decoded, resized and written as WebP.tainted bytes $thumbnail = Image::open($upload) ->resized({ width: 150, height: 150 }) ->withFormat(Format::Webp, { quality: 75 }) ->encode();
// This prints webp 150x150.var $info = Image::info($thumbnail);echo Image::extension($info->format), ' ', $info->width, 'x', $info->height, "\n";webp 150x150Check the file signature
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows that `encode` returns the bytes of a real file, ready to save or to send.use Novis\Image\Image;
tainted bytes $png = Image::create(10, 10, Color::hex('#336699'))->withFormat(Format::Png)->encode();
// Every PNG file starts with the same four bytes. This prints 89504e47.echo Core\Encoding::toHex(Core\Bytes::slice($png, 0, 4)), "\n";89504e47Errors appear at encode
<?nvsuse Novis\Image\Color;// Shows that a step that cannot run throws its error when `encode` runs, not when you add the step.use Novis\Image\Image;
Image $photo = Image::create(100, 100, Color::hex('#336699'));
// The crop box is outside the 100 by 100 image. Adding the step does not throw.Image $cropped = $photo->cropped({ x: 50, y: 50, width: 100, height: 100 });echo "step added\n";
// `encode` runs the step and throws an error.try { $cropped->encode();} catch (Throwable $e) { echo $e->message, "\n";}step added`cropped` of 100x100 at 50, 50 does not lie inside the 100x100 image