API / Minn\Media / Canvas

Canvas final readonly class

Minn\Media\Canvas · public/minn/src/Minn/Media/Canvas.php · 168 lines

One GD bitmap and the operations the media layer needs on it. Every operation returns a new canvas; alpha is preserved throughout, which is what makes PNG and WebP sub-sizes match the reference's.

Constants and properties

privatereadonly GdImage $image

Promoted from the constructor.

propreadonly int $width

Promoted from the constructor.

propreadonly int $height

Promoted from the constructor.

Public

new__construct(GdImage $image, int $width, int $height)#
staticof(GdImage $image): self#

A canvas over a GD image already in hand (what WP_Image_Editor_GD::_save is given).

methodimage(): GdImage#

The GD image behind the canvas (what WP_Image_Editor_GD::_resize hands back).

staticopen(string $path): ?self#

A canvas from an image file, or null when it cannot be decoded or would not fit in memory. The header names the dimensions before a pixel is decoded, so a small file that claims a huge canvas is refused instead of taking the request down.

methodresample(array $box): self#

Resamples a source box onto a destination box.

$boxarray{0: int, 1: int, 2: int, 3: int, 4: int, 5: int, 6: int, 7: int}dst x, dst y, src x, src y, dst w, dst h, src w, src h (the GD argument order)
methodcrop(int $x, int $y, int $width, int $height, ?int $targetWidth = NULL, ?int $targetHeight = NULL): self#

A canvas cut to a rectangle, resized to a target when given.

methodrotate(float $angle): ?self#

A canvas turned by an angle, or null when GD refuses.

methodflipVertical(): self#

A canvas mirrored on either axis.

methodflipHorizontal(): self#

A canvas mirrored left to right.

methodcopy(): self#

An untouched copy of the canvas.

methodwrite(string $path, string $mime, int $quality): bool#

Writes the bitmap in the given format; the directory is created when missing.

methodprogressive(): self#

The bitmap written progressively from now on (an interlaced JPEG).

methodbaseline(): self#

The bitmap written line by line from now on, as it is by default.

methodstream(string $mime, int $quality): bool#

Writes the image to the output in a format, at a quality.

Internals 4

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privateaffordable(int $width, int $height): bool#

Whether a bitmap of these dimensions fits the memory left to this request: five bytes a pixel for the decoded image (GD keeps four and a little), twice over because every operation makes a second canvas, under what memory_limit leaves after what is already in use.

privatememoryLimit(): ?int#

memory_limit in bytes, or null when it is unlimited.

privateflipped(int $mode): self#
privateencode(string $mime, ?string $path, int $quality): bool#