Novis\Image\Image
An image and a list of steps to run on it. Each step method returns a new Image and leaves this one as it was. Nothing is decoded until encode, encodeVariants or raw runs.
Novis\Image\Image is an image and a list of steps to run on it. Each step method, such as resized, cropped or withFormat, returns a new Image and does not change the old one. No pixel is decoded until encode, encodeVariants or raw runs the steps, so bytes that are not an image throw a ParseError only then. open, info, hash, placeholder, palette and compare read an encoded image from bytes, an open Core\IO\File or an uploaded Core\Request\Part. encode removes all metadata, such as the place a photo was taken, unless keepingMetadata is a step.
<?nvsuse Novis\Image\Format;use Novis\Image\Image;
// A PNG of 3 by 2 pixels, written as hex.bytes $png = Core\Encoding::fromHex( '89504e470d0a1a0a0000000d49484452000000030000000208060000009d74661a' . '0000000b49444154789c6360c00500001a0001bc3ce0410000000049454e44ae426082',);
Image $photo = Image::open($png);// `encode` returns `tainted bytes`, because an image can come from an upload.tainted bytes $thumb = $photo->resized({ width: 6, upscale: true })->withFormat(Format::Jpeg, { quality: 80 })->encode();
var $info = Image::info($thumb);echo Image::mime($info->format), ' ', $info->width, 'x', $info->height, "\n";image/jpeg 6x4Members
32 members.
| Member | Signature |
|---|---|
open | open(Core\Bytes::Source $data, ?{ autoOrient?: bool, toSrgb?: bool, maxPixels?: uint, page?: uint, dpi?: float } $options = null): Image |
create | create(uint $width, uint $height, ?Color $fill = null): Image |
fromRaw | fromRaw(Raw $raw): Image |
info | info(Core\Bytes::Source $data): Info |
mime | mime(Format $format): string |
extension | extension(Format $format): string |
resized | resized(Resize $options): Image |
cropped | cropped({ x: uint, y: uint, width: uint, height: uint } $box): Image |
trimmed | trimmed(?{ threshold?: float } $options = null): Image |
rotated | rotated(float $degrees, ?{ background?: Color } $options = null): Image |
flipped | flipped(Axis $axis): Image |
withOverlay | withOverlay(Image $overlay, ?{ gravity?: Gravity, x?: int, y?: int, opacity?: float, blend?: Blend } $options = null): Image |
flattened | flattened(Color $background): Image |
sharpened | sharpened(?{ sigma?: float } $options = null): Image |
blurred | blurred(float $sigma): Image |
grayscaled | grayscaled(): Image |
withBrightness | withBrightness(float $amount): Image |
withContrast | withContrast(float $amount): Image |
withGamma | withGamma(float $gamma): Image |
tinted | tinted(Color $color): Image |
withText | withText(string $text, Font $font, { size: float, color: Color, gravity?: Gravity, x?: int, y?: int, maxWidth?: uint, align?: Align } $options): Image |
measureText | measureText(string $text, Font $font, { size: float, maxWidth?: uint, align?: Align } $options): Size |
withFormat | withFormat(Format $format, ?{ quality?: uint, progressive?: bool, lossless?: bool, effort?: uint } $options = null): Image |
keepingMetadata | keepingMetadata(): Image |
encode | encode(): tainted bytes |
raw | raw(): Raw |
encodeVariants | encodeVariants(array<Variant> $set): array<tainted bytes> |
compare | compare(Core\Bytes::Source|Image $a, Core\Bytes::Source|Image $b, ?{ tolerance?: uint, render?: bool } $options = null): Comparison |
hash | hash(Core\Bytes::Source $data, HashKind $kind): bytes |
hashDistance | hashDistance(bytes $a, bytes $b): uint |
placeholder | placeholder(Core\Bytes::Source $data, PlaceholderKind $kind): string |
palette | palette(Core\Bytes::Source $data, uint $count = 5): array<Color> |
Enums
Gravity { Center, North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest }A position in an image, written as a direction from the center. Image programs call this position the gravity.Northis the top,Eastis the right,Southis the bottom andWestis the left.resized,withOverlayandwithTextuse it to place the image or the text. Whenresizedcuts off part of the image, it keeps the part at this position. The default isCenter.Fit { Cover, Contain, Fill, Inside, Outside }Howresizedfits an image into a box of the width and height you give.Coverfills the whole box and cuts off what does not fit. It is the default.Containshows the whole image in the box, and the empty part is transparent.Fillstretches the image to the size of the box, so its shape changes.Insidekeeps the whole image and its shape, and the result is no larger than the box.Outsidekeeps the whole image and its shape, and the result is no smaller than the box.Filter { Nearest, Bilinear, CatmullRom, Mitchell, Lanczos3 }The filterresizeduses to calculate the new pixels. A faster filter gives a softer result, and a slower filter keeps more detail.Nearestis the fastest. It copies the closest pixel and keeps hard edges, so it suits pixel art.Bilinearis fast, and the result is a little soft.CatmullRomandMitchellare slower and sharper thanBilinear.Lanczos3is the slowest and keeps the most detail. It is the default.Axis { Horizontal, Vertical }The direction in whichflippedmirrors an image.Horizontalswaps left and right, andVerticalswaps top and bottom.Blend { Normal, Multiply, Screen, Overlay, Darken, Lighten }HowwithOverlaymixes the colours of the image on top with the colours of the image below it.Normalputs the top image over the lower one. It is the default.Multiplymakes the result darker. White on top changes nothing.Screenmakes the result lighter. Black on top changes nothing.Overlaymakes dark parts darker and light parts lighter, so the contrast is higher.Darkenkeeps the darker of the two values for each of red, green and blue.Lightenkeeps the lighter of the two values for each of red, green and blue.Align { Left, Center, Right }HowwithTextandImage::measureTextline up the lines of a text. The default isLeft.HashKind { Perceptual, Difference, Average }The kind of hashImage::hashreturns. An image hash is a short value that describes what an image looks like. Two similar images have almost the same hash. APerceptualhash is 8 bytes and is based on the large shapes of the image. ADifferencehash is 16 bytes and is based on where the image gets brighter or darker. AnAveragehash is 32 bytes and is based on which parts are brighter than the image as a whole.PlaceholderKind { BlurHash, ThumbHash }The kind of placeholderImage::placeholderreturns. A placeholder is a short text that a web page draws as a blurred preview while the real image loads.BlurHashandThumbHashare two formats for this text. The browser decodes each format with a small script made for it. ABlurHashis 28 characters and has no transparency. AThumbHashis base64 text and also keeps transparency.
