Framework Docs Image

Image

The Image class wraps the PHP GD extension with a fluent API for loading, resizing, cropping, and saving images in any format supported by the server (JPEG, PNG, GIF, WebP, BMP, AVIF).

Namespace: Wojo\Core\Image

Loading Images

Method Signature Description
__construct() new Image(string $filename) Load from a filesystem path. Throws RuntimeException if a file not found or unreadable.
createFromString() static createFromString(string $imageData): self Load from raw binary image data (e.g., from a database BLOB)

Resizing

Method Signature Description
resize() resize(int $w, int $h, bool $allowEnlarge = false): static Resize to exact pixel dimensions
resizeToWidth() resizeToWidth(int $w, bool $allowEnlarge = false): static Scale to width, preserve an aspect ratio
resizeToHeight() resizeToHeight(int $h, bool $allowEnlarge = false): static Scale to height, preserve an aspect ratio
resizeToShortSide() resizeToShortSide(int $max, bool $allowEnlarge = false): static Constrain the shorter dimension
resizeToLongSide() resizeToLongSide(int $max, bool $allowEnlarge = false): static Constrain the longer dimension
bestFit() bestFit(int $maxW, int $maxH, bool $allowEnlarge = false): static Shrink proportionally to fit a bounding box
scale() scale(int $percent): static Scale to a percentage of original dimensions

Cropping

Method Signature Description
crop() crop(int $w, int $h, bool $allowEnlarge = false, int $position = CROPCENTER): static Smart crop to exact size. Position constants: CROPTOP, CROPCENTER, CROPBOTTOM, CROPLEFT, CROPRIGHT, CROPTOPCENTER
freeCrop() freeCrop(int $w, int $h, int|false $x = false, int|false $y = false): static Crop from an explicit X/Y offset, or center-crop when omitted
thumbnail() thumbnail(int $w, ?int $h = null, bool $allowEnlarge = false): static Generate a thumbnail that fills the given box (may crop)

Saving & Output

Method Signature Description
save() save(?string $filename, ?int $imageType, ?int $quality, ?int $permissions, array|bool $exactSize): static Save to disk in any GD-supported type
output() output(?int $imageType, ?int $quality): void Stream image directly to the browser with a Content-Type header
getImageAsString() getImageAsString(?int $imageType, ?int $quality): string Return image as a raw binary string
addFilter() addFilter(callable $filter): static Queue a GD filter callback (applied on save/output)
getSourceWidth() / getSourceHeight() : int Original image dimensions
getDestWidth() / getDestHeight() : int Output dimensions after resize/crop

Quality Settings

Public properties that control output quality (set before calling save/output):

Property Default Description
$quality_jpg 85 JPEG quality 0–100
$quality_webp 85 WebP quality 0–100
$quality_png 9 PNG compression 0–9
$interlace true Progressive/interlaced output
$gamma_correct false Apply gamma correction during resampling

Example

PHP
// Resize an uploaded image and save two sizes
$img = new Image(UPLOADPATH . 'original.jpg');

// Full width up to 1200px
$img->bestFit(1200, 1200)
    ->save(UPLOADPATH . 'large.webp', IMAGETYPE_WEBP, 82);

// 300×300 thumbnail, center-cropped
(new Image(UPLOADPATH . 'original.jpg'))
    ->thumbnail(300, 300)
    ->save(UPLOADPATH . 'thumb.webp', IMAGETYPE_WEBP);

// Stream directly to browser
header('Cache-Control: max-age=86400');
(new Image(UPLOADPATH . 'photo.jpg'))
    ->resizeToWidth(800)
    ->output(IMAGETYPE_WEBP);