Skip to content

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.

<?nvs
use 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";
Output
image/jpeg 6x4

Members

32 members.

MemberSignature
openopen(Core\Bytes::Source $data, ?{ autoOrient?: bool, toSrgb?: bool, maxPixels?: uint, page?: uint, dpi?: float } $options = null): Image
createcreate(uint $width, uint $height, ?Color $fill = null): Image
fromRawfromRaw(Raw $raw): Image
infoinfo(Core\Bytes::Source $data): Info
mimemime(Format $format): string
extensionextension(Format $format): string
resizedresized(Resize $options): Image
croppedcropped({ x: uint, y: uint, width: uint, height: uint } $box): Image
trimmedtrimmed(?{ threshold?: float } $options = null): Image
rotatedrotated(float $degrees, ?{ background?: Color } $options = null): Image
flippedflipped(Axis $axis): Image
withOverlaywithOverlay(Image $overlay, ?{ gravity?: Gravity, x?: int, y?: int, opacity?: float, blend?: Blend } $options = null): Image
flattenedflattened(Color $background): Image
sharpenedsharpened(?{ sigma?: float } $options = null): Image
blurredblurred(float $sigma): Image
grayscaledgrayscaled(): Image
withBrightnesswithBrightness(float $amount): Image
withContrastwithContrast(float $amount): Image
withGammawithGamma(float $gamma): Image
tintedtinted(Color $color): Image
withTextwithText(string $text, Font $font, { size: float, color: Color, gravity?: Gravity, x?: int, y?: int, maxWidth?: uint, align?: Align } $options): Image
measureTextmeasureText(string $text, Font $font, { size: float, maxWidth?: uint, align?: Align } $options): Size
withFormatwithFormat(Format $format, ?{ quality?: uint, progressive?: bool, lossless?: bool, effort?: uint } $options = null): Image
keepingMetadatakeepingMetadata(): Image
encodeencode(): tainted bytes
rawraw(): Raw
encodeVariantsencodeVariants(array<Variant> $set): array<tainted bytes>
comparecompare(Core\Bytes::Source|Image $a, Core\Bytes::Source|Image $b, ?{ tolerance?: uint, render?: bool } $options = null): Comparison
hashhash(Core\Bytes::Source $data, HashKind $kind): bytes
hashDistancehashDistance(bytes $a, bytes $b): uint
placeholderplaceholder(Core\Bytes::Source $data, PlaceholderKind $kind): string
palettepalette(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. North is the top, East is the right, South is the bottom and West is the left. resized, withOverlay and withText use it to place the image or the text. When resized cuts off part of the image, it keeps the part at this position. The default is Center.
  • Fit { Cover, Contain, Fill, Inside, Outside }How resized fits an image into a box of the width and height you give. Cover fills the whole box and cuts off what does not fit. It is the default. Contain shows the whole image in the box, and the empty part is transparent. Fill stretches the image to the size of the box, so its shape changes. Inside keeps the whole image and its shape, and the result is no larger than the box. Outside keeps the whole image and its shape, and the result is no smaller than the box.
  • Filter { Nearest, Bilinear, CatmullRom, Mitchell, Lanczos3 }The filter resized uses to calculate the new pixels. A faster filter gives a softer result, and a slower filter keeps more detail. Nearest is the fastest. It copies the closest pixel and keeps hard edges, so it suits pixel art. Bilinear is fast, and the result is a little soft. CatmullRom and Mitchell are slower and sharper than Bilinear. Lanczos3 is the slowest and keeps the most detail. It is the default.
  • Axis { Horizontal, Vertical }The direction in which flipped mirrors an image. Horizontal swaps left and right, and Vertical swaps top and bottom.
  • Blend { Normal, Multiply, Screen, Overlay, Darken, Lighten }How withOverlay mixes the colours of the image on top with the colours of the image below it. Normal puts the top image over the lower one. It is the default. Multiply makes the result darker. White on top changes nothing. Screen makes the result lighter. Black on top changes nothing. Overlay makes dark parts darker and light parts lighter, so the contrast is higher. Darken keeps the darker of the two values for each of red, green and blue. Lighten keeps the lighter of the two values for each of red, green and blue.
  • Align { Left, Center, Right }How withText and Image::measureText line up the lines of a text. The default is Left.
  • HashKind { Perceptual, Difference, Average }The kind of hash Image::hash returns. An image hash is a short value that describes what an image looks like. Two similar images have almost the same hash. A Perceptual hash is 8 bytes and is based on the large shapes of the image. A Difference hash is 16 bytes and is based on where the image gets brighter or darker. An Average hash is 32 bytes and is based on which parts are brighter than the image as a whole.
  • PlaceholderKind { BlurHash, ThumbHash }The kind of placeholder Image::placeholder returns. A placeholder is a short text that a web page draws as a blurred preview while the real image loads. BlurHash and ThumbHash are two formats for this text. The browser decodes each format with a small script made for it. A BlurHash is 28 characters and has no transparency. A ThumbHash is base64 text and also keeps transparency.

Types

Resize

type Resize = {    width?: uint,    height?: uint,    fit?: Fit,    gravity?: Gravity,    filter?: Filter,    upscale?: bool,};

Raw

type Raw = { width: uint, height: uint, pixels: tainted bytes };

Info

type Info = {    format: Format,    width: uint,    height: uint,    hasAlpha: bool,    frames: uint,    orientation: uint,    exif: ?array<tainted string>,    hasIcc: bool,};

Variant

type Variant = {    width?: uint,    height?: uint,    fit?: Fit,    format?: Format,    quality?: uint,};

Size

type Size = { width: uint, height: uint };