Skip to content

Options

Immutable configuration value objects for loading, saving, and text rendering.


LoadOptions

Options controlling how an image is loaded from disk or buffer. Immutable — use with* methods to derive variants.

php
final class LoadOptions

Constructor

php
public function __construct(
    public readonly ?int $page = null,
    public readonly ?int $n = null,
    public readonly bool $autoRotate = true,
    public readonly ?int $shrink = null,
    public readonly ?float $scale = null,
)

Properties

PropertyTypeDefaultDescription
$page?intnullPage number to load (for multipage formats like TIFF)
$n?intnullNumber of pages to load
$autoRotatebooltrueWhether to auto-rotate based on EXIF orientation
$shrink?intnullShrink-on-load factor (integer downscale)
$scale?floatnullScale-on-load factor (float downscale)

Static Methods

default()

Creates a LoadOptions with all defaults.

php
public static function default(): self

Mutator Methods

Each returns a new instance with the modified property.

withPage()

Set the page (zero-based) to load for multi-page formats.

php
public function withPage(int $page): self

withN()

Set the number of pages to load.

php
public function withN(int $n): self

withAutoRotate()

Enable or disable auto-rotation based on EXIF orientation.

php
public function withAutoRotate(bool $autoRotate): self

withShrink()

Set the shrink-on-load factor (integer downscale).

php
public function withShrink(int $factor): self

withScale()

Set the scale-on-load factor (float downscale).

php
public function withScale(float $scale): self

Internal Methods

toVipsOptions()

Convert to a VIPS-compatible options array.

php
public function toVipsOptions(): array

Returns: Associative array for VIPS newFromFile / newFromBuffer options.

Usage

php
use PhpMlKit\Opal\Image;
use PhpMlKit\Opal\LoadOptions;

// Default loading
$img = Image::fromFile('photo.jpg');

// Load second page of a TIFF
$options = (new LoadOptions())->withPage(1);
$tiffPage = Image::fromFile('multi-page.tif', $options);

// Load with shrink-on-load (faster thumbnails)
$options = LoadOptions::default()
    ->withShrink(2)
    ->withAutoRotate(false);
$img = Image::fromFile('large.jpg', $options);

SaveOptions

Options controlling how an image is saved. Format-specific. Use one of the static factory methods to get a pre-configured instance.

php
final class SaveOptions

Static Factory Methods

jpeg()

php
public static function jpeg(
    int $quality = 85,
    bool $strip = true,
    bool $progressive = false,
): self
ParameterTypeDefaultDescription
$qualityint85JPEG quality (0–100)
$stripbooltrueRemove metadata
$progressiveboolfalseEnable progressive JPEG

png()

php
public static function png(
    int $compression = 6,
    bool $strip = true,
    bool $interlace = false,
): self
ParameterTypeDefaultDescription
$compressionint6PNG compression level (0–9)
$stripbooltrueRemove metadata
$interlaceboolfalseEnable Adam7 interlacing

webp()

php
public static function webp(
    int $quality = 80,
    bool $lossless = false,
    bool $strip = true,
): self
ParameterTypeDefaultDescription
$qualityint80WebP quality (0–100)
$losslessboolfalseEnable lossless compression
$stripbooltrueRemove metadata

tiff()

php
public static function tiff(int $quality = 75, bool $strip = false): self
ParameterTypeDefaultDescription
$qualityint75TIFF quality
$stripboolfalseRemove metadata

avif()

php
public static function avif(int $quality = 50, int $speed = 5): self
ParameterTypeDefaultDescription
$qualityint50AVIF quality (0–100)
$speedint5Encoding speed (0=slow/best, 8=fast/worst)

heif()

php
public static function heif(int $quality = 50): self
ParameterTypeDefaultDescription
$qualityint50HEIF quality (0–100)

Mutator Methods

withQuality()

Set the encoding quality (0–100).

php
public function withQuality(int $quality): self

withStrip()

Set whether to strip metadata on save.

php
public function withStrip(bool $strip): self

Internal Methods

toVipsOptions()

Convert to a VIPS-compatible options array.

php
public function toVipsOptions(): array

Usage

php
use PhpMlKit\Opal\Image;
use PhpMlKit\Opal\SaveOptions;

$img = Image::fromFile('input.jpg');

// Save as JPEG with high quality
$img->toFile('output.jpg', SaveOptions::jpeg(quality: 95));

// Save as lossless WebP
$img->toFile('output.webp', SaveOptions::webp(lossless: true));

// Encode to PNG buffer with custom compression
$png = $img->toBuffer(
    ImageFormat::PNG,
    SaveOptions::png(compression: 9, interlace: true)
);

TextOptions

Options controlling how text is rendered. Immutable — use with* methods to derive variants.

php
final class TextOptions

Constructor

php
public function __construct(
    public readonly ?string $font = null,
    public readonly ?string $fontFile = null,
    public readonly ?int $fontSize = null,
    public readonly ?int $width = null,
    public readonly ?int $height = null,
    public readonly ?string $align = null,
    public readonly ?bool $justify = null,
    public readonly ?int $dpi = null,
    public readonly ?bool $rgba = null,
    public readonly ?int $spacing = null,
    public readonly ?string $wrap = null,
)

Properties

PropertyTypeDefaultDescription
$font?stringnullFont family name (e.g. 'sans-serif', 'serif', 'monospace')
$fontFile?stringnullPath to a custom font file
$fontSize?int12Font size in points
$width?intnullMaximum width in pixels for text wrapping
$height?intnullMaximum height in pixels
$align?stringnullText alignment ('left', 'centre', 'right')
$justify?boolnullWhether to justify text
$dpi?intnullRendering resolution in DPI
$rgba?boolnullWhether to render as RGBA
$spacing?intnullLine spacing in points
$wrap?stringnullWrapping mode ('word', 'char', 'word-char')

Static Methods

default()

php
public static function default(): self

Creates a TextOptions with font size 12 and all other values as null.

Mutator Methods

Each returns a new instance with the modified property.

withFont()

Set the font family or Pango font description.

php
public function withFont(string $font): self

withFontfile()

Set a custom font file path (.ttf or .otf).

php
public function withFontFile(string $fontFile): self

withFontSize()

Set the font size in points.

php
public function withFontSize(int $fontSize): self

withWidth()

Set the maximum text block width in pixels.

php
public function withWidth(int $width): self

withHeight()

Set the maximum text block height in pixels.

php
public function withHeight(int $height): self

withAlign()

Set alignment: 'left', 'centre', or 'right'.

php
public function withAlign(string $align): self

withJustify()

Enable or disable text justification.

php
public function withJustify(bool $justify): self

withDpi()

Set rendering resolution in DPI.

php
public function withDpi(int $dpi): self

withRgba()

Enable RGBA rendering for transparent backgrounds.

php
public function withRgba(bool $rgba = true): self

withSpacing()

Set line spacing in points.

php
public function withSpacing(int $spacing): self

withWrap()

Set wrapping mode: 'word', 'char', or 'word-char'.

php
public function withWrap(string $wrap): self

Internal Methods

toVipsOptions()

Convert to a VIPS-compatible options array.

php
public function toVipsOptions(): array

Returns: Associative array for VIPS text operation options.

Usage

php
use PhpMlKit\Opal\Image;
use PhpMlKit\Opal\TextOptions;

// Create a text image with defaults
$textImg = Image::text('Hello World');

// Custom text rendering
$options = TextOptions::default()
    ->withFont('sans-serif')
    ->withFontSize(48)
    ->withWidth(800)
    ->withAlign('centre')
    ->withDpi(300);

$textImg = Image::text('Hello World', $options);

// Draw text onto an existing image
$img = Image::fromFile('photo.jpg');
$img = $img->drawText('Hello', 100, 200, color: Color::white(), options: $options);

Released under the MIT License.