Search by

spline / image

splinephp

Image processing for PHP: dimensions and EXIF, resizing and cropping with GD, ImageMagick or libvips

v0.1.0 2026-12-01 17:12 UTC

This package is auto-updated.

Last update: 2026-12-01 17:14:48 UTC


README

CI License: MIT codecov

Image processing for PHP: tools for reading image dimensions and metadata, resizing and cropping images with GD, ImageMagick or libvips, organising files across named storage disks, and describing image variants that are generated on demand. Everything lives under Spline\Image.

Installation

composer require spline/image

Requirements

  • PHP 8.4 or higher with ext-mbstring
  • ext-fileinfo to detect MIME types from file contents (without it, the extension is used)
  • ext-exif to read EXIF metadata and auto-orient photos
  • For the GD darkroom: ext-gd
  • For the ImageMagick darkroom: ImageMagick 7, whose magick binary is the default bin option
  • For the libvips darkroom: libvips, ext-ffi and jcupitt/vips. Colour profiles are kept from libvips 8.15; older versions strip them with the other metadata

Dimensions

Spline\Image\Dimensions reads and manipulates image dimensions. It does not modify the actual file — it calculates what the new dimensions would be.

use Spline\Image\Dimensions;

$dims = Dimensions::forImage('/path/to/photo.jpg');

$dims->width();       // int
$dims->height();      // int
$dims->ratio();       // float — width / height, 0.0 when either side is 0
$dims->landscape();   // bool
$dims->portrait();    // bool
$dims->square();      // bool
$dims->orientation(); // 'landscape', 'portrait' or 'square'; false when ratio() is 0

forImage() returns a 0 × 0 instance for a path that does not exist. You can also construct one directly: new Dimensions(1200, 768).

Resizing and cropping

These methods mutate the instance and return $this. Calling several in a row compounds — each call starts from the result of the previous one. Clone first, or construct a fresh instance per calculation, when you need more than one result from the same source.

$source = Dimensions::forImage('/path/to/photo.jpg'); // e.g. 1920 × 1080

// Fit within 800×600, preserving aspect ratio
$resized = (clone $source)->resize(800, 600);        // 800 × 450

// Scale to exactly 400 wide, height follows
$resized = (clone $source)->fitWidth(400);           // 400 × 225

// Scale to exactly 300 tall, width follows
$resized = (clone $source)->fitHeight(300);          // 533 × 300

// Fit within a square box
$resized = (clone $source)->fit(512);                // 512 × 288

// Crop to exact dimensions (no aspect calculation — sets both sides)
$cropped = (clone $source)->crop(400, 300);          // 400 × 300

// Same as resize()/crop(), driven by an options array
$thumb = (clone $source)->thumb(['width' => 200, 'height' => 200, 'crop' => true]);

Without the clone, $source itself would be 400 × 300 after the last call.

fitWidth(), fitHeight(), fit() and resize() never upscale: if the source already fits, the instance is left as it is. Pass force: true to scale up:

$resized = (clone $source)->fitWidth(2000, force: true);

Convert to array:

$dims->toArray();
// ['width' => 1920, 'height' => 1080, 'ratio' => 1.7777777777777777, 'orientation' => 'landscape']

For SVG files:

$dims = Dimensions::forSvg('/path/to/icon.svg');

forSvg() reads the root element's width/height attributes and falls back to the viewBox when they are missing or percentages. It parses with LIBXML_NONET and reads at most 1 MB, so it is safe on uploads.

File and Image

Spline\Image\File wraps a file on disk together with the disk it belongs to and the Storage that decides where its generated variants live. Spline\Image\Image extends it with everything that only makes sense for images. Both implement FileInterface.

use Spline\Image\Image;

$image = new Image('/var/www/app/uploads/photo.jpg', 'default', $storage, $diskManager);

$image->root();          // '/var/www/app/uploads/photo.jpg'
$image->filename();      // 'photo.jpg'
$image->name();          // 'photo'
$image->extension();     // 'jpg'
$image->mime();          // 'image/jpeg' or null
$image->disk();          // DiskInterface
$image->width();         // int
$image->height();        // int
$image->dimensions();    // Dimensions (cached)
$image->orientation();   // as Dimensions::orientation()
$image->isResizable();   // jpg, jpeg, gif, png, webp — and the bytes must match the extension
$image->isViewable();    // the resizable types plus avif and svg
$image->exif();          // Exif (see below)
$image->toArray();       // ['dimensions' => [...], 'exif' => [...]]

The constructor throws ImageException when the path does not exist, is not a regular file, or is not readable. The disk argument is either a DiskInterface or a disk name; a name is resolved through the DiskManager you pass (or a fresh one, which only knows default). Without a Storage the file gets new Storage() with its /media and /uploads defaults.

Image::isResizable() checks the extension and then verifies the real MIME type against it. A .jpg whose bytes are not a JPEG throws ImageException::mimeTypeMismatch() rather than returning false, so a renamed upload cannot slip through. The plain File::isResizable() only looks at the extension.

Exif, Camera and Location

Image::exif() returns a Spline\Image\Exif built from exif_read_data(). It needs ext-exif, which is in composer.json suggest. Without it exif_read_data() is skipped: exposure(), aperture(), iso(), focalLength(), isColor() and the GPS values are null, camera() and location() return empty objects, orientation() is 1, and timestamp() falls back to the file's modification time.

$exif = $image->exif();

$exif->camera();        // Camera — make(), model(), (string) $camera → 'Make Model'
$exif->location();      // Location — lat(), lng() as floats or null; (string) → 'lat, lng'
$exif->timestamp();     // ?string — DateTimeOriginal, else FileDateTime, else mtime
$exif->exposure();      // ?string — the raw ExposureTime tag
$exif->aperture();      // ?string — COMPUTED ApertureFNumber
$exif->iso();           // ?int — the first value when the camera records several
$exif->orientation();   // int — the EXIF Orientation, 1 to 8; 1 when there is none
$exif->focalLength();   // ?string
$exif->isColor();       // ?bool
$exif->data();          // the raw array

toArray() leaves GPS out unless you ask for it:

$exif->toArray();                        // camera, timestamp, exposure, aperture, iso, focalLength, isColor
$exif->toArray(includeLocation: true);   // adds 'location' => ['lat' => …, 'lng' => …]

Image::toArray() takes the same $includeLocation flag and passes it through. Treat location as personal data: do not surface it by default.

Camera and Location can also be constructed from any raw EXIF array — new Camera($data), new Location($data) — which is how their tests exercise them. Location reads GPSLatitude/GPSLatitudeRef/GPSLongitude/GPSLongitudeRef and converts the degree/minute/second rationals to signed decimals.

A photo taken in portrait is often stored landscape, with an orientation of 5 to 8 that turns it upright on display. Image::dimensions(), width() and height() report the upright size, which is the size the darkrooms produce: all three auto-orient the image before they resize or crop it. With 'autoOrient' => false the darkroom plans from the stored size instead.

Darkroom

Spline\Image\Darkroom processes image files. It is abstract — choose a driver based on what is available on your server. All drivers implement DarkroomInterface, which is what you should type-hint against so the driver can be swapped in configuration.

Driver Class Requires
GD Spline\Image\Darkroom\GdLib ext-gd, a suggest entry in composer.json. Writing WebP, AVIF or BMP also needs a GD build with that format. process() throws a \RuntimeException naming the missing piece.
ImageMagick Spline\Image\Darkroom\ImageMagick The magick CLI on the PATH (override with the bin option).
libvips Spline\Image\Darkroom\Vips libvips, ext-ffi and the jcupitt/vips package (composer require jcupitt/vips). FFI must be allowed outside preloading: PHP's default ffi.enable=preload permits it on the CLI only, so set ffi.enable=true for PHP-FPM. process() throws a \RuntimeException naming the missing piece.

The libvips driver is the fastest and lightest on memory: a plain resize or a centre crop shrinks the image while it is decoded. It keeps the colour profile and drops all other metadata, also for PNG. Like GD, it keeps only the first frame of an animated GIF or WebP; use ImageMagick to keep animations. It accepts an interlace option for progressive JPEGs and interlaced PNGs, as ImageMagick does.

Large sources are refused before they are decoded. A PNG of 20,000 × 20,000 pixels can be a few kilobytes on disk and gigabytes in memory once decoded. preprocess() reads the width and height from the file header and throws ImageException when they exceed the darkroom's maxPixels setting. The default is Darkroom::MAX_PIXELS, 50 megapixels. Raise it for cameras that shoot more, or pass null to turn the check off. It is a setting of the darkroom, so a per-image option or a stored job cannot raise it:

$darkroom = new GdLib(['maxPixels' => 120_000_000]);   // a 100-megapixel medium-format camera

If you downscale masters on upload, give that darkroom a limit that fits your cameras. If the downscale is refused, the master stays full size and every variant made from it is refused too.

process() writes the result over the file you pass in and returns the normalised option array it acted on — not a path. Copy the original somewhere first when you need to keep it:

use Spline\Image\Darkroom\GdLib;

$darkroom = new GdLib();

copy('/path/to/original.jpg', '/path/to/thumb.jpg');

$options = $darkroom->process('/path/to/thumb.jpg', [
    'width'   => 800,
    'height'  => 600,
    'crop'    => true,
    'quality' => 85,
]);

$options['width'];   // 800 — the dimensions actually produced
$options['crop'];    // 'center' — `true` normalised to the anchor

The one exception to "same path": ImageMagick and Vips with a format option write to a sibling file with the new extension (thumb.jpg → thumb.webp). GdLib re-encodes in the new format but keeps the path you gave it.

Options you pass override the driver's defaults. You can also set defaults once in the constructor — new GdLib(['quality' => 80, 'autoOrient' => false]) — and every process() call starts from them.

Option Type Default Description
width ?int null Target width in pixels
height ?int null Target height in pixels
crop bool|string false false fits inside the box; true is 'center'; an anchor — 'top left', 'top', 'top right', 'left', 'center', 'right', 'bottom left', 'bottom', 'bottom right'; or a focal point such as '30%,60%' (see Focus)
quality int 90 JPEG, WebP and AVIF quality (1–100); Vips also applies it to HEIC and JPEG XL
format ?string null Re-encode as jpg, png, webp, …
grayscale bool false Convert to greyscale. greyscale and bw are accepted as aliases
blur bool|int false Gaussian blur radius; true means 10
sharpen bool|int null Sharpen amount; true means 50
autoOrient bool true Rotate according to the EXIF orientation tag

Some drivers take extra options:

Option Drivers Default Description
interlace ImageMagick, Vips false Progressive JPEG; Vips also interlaces PNG
bin ImageMagick 'magick' The ImageMagick binary
threads ImageMagick 1 Threads ImageMagick may use

preprocess() is the first half of process() on its own: it merges the options with the defaults, normalises the shorthands above, reads the source dimensions and works out the final width/height (plus sourceWidth, sourceHeight, scaleWidth, scaleHeight). It does not touch the file. It is on DarkroomInterface because anything that replays a stored job needs it before deciding on an output filename:

$options = $darkroom->preprocess('/path/to/image.jpg', ['width' => 400]);
// ['width' => 400, 'height' => 267, 'sourceWidth' => 1200, 'crop' => false, …]

Focus

Spline\Image\Focus is the static helper behind focal-point crops. A focal point is a string with two percentages — '30%,60%', '30%, 60%' or the legacy JSON '{"x":0.3,"y":0.6}' — marking the spot that must stay in frame.

use Spline\Image\Focus;

Focus::isFocalPoint('30%,60%');   // true — anything containing '%'
Focus::parse('30%,60%');          // [0.3, 0.6]
Focus::normalize('top left');     // '0% 0%' — named positions, plugin JSON and focal points
                                  // all become an "x% y%" string; anything else is '50% 50%'
Focus::ratio(1200, 700);          // 1.7142857142857142; 0.0 when height is 0

// Crop box in source pixels for a 400×350 thumb of a 1200×700 source,
// keeping the point at (0%, 0%) — the top-left — in frame:
Focus::coords('0%, 0%', 1200, 700, 400, 350);
// ['x1' => 0, 'y1' => 0, 'x2' => 800, 'y2' => 700, 'width' => 800, 'height' => 700]

Named positions are the Spline\Image\Gravity cases: top left, top, top right, left, center, right, bottom left, bottom and bottom right. Gravity::from('top')->toPercentageString() is '50% 0%'. Plugin JSON with a missing or non-numeric x or y is read as the center.

coords() returns null when the source and target already have the same ratio, so no crop is needed. The Darkroom drivers call it whenever the crop option looks like a focal point.

DiskManager

Spline\Image\DiskManager organises file storage into named disks. Each disk has a root directory and an optional public URL. The concept is inspired by Laravel Filesystem and Flysystem.

A new manager already contains one disk, default, rooted at /uploads with no URL, and it is the default disk until you say otherwise. Register yours on top of it:

use Spline\Image\Disk;
use Spline\Image\DiskManager;

$manager = new DiskManager();

$manager->registerMultiple([
    'avatars'   => ['root' => '/var/www/app/storage/avatars',   'url' => 'https://example.com/storage/avatars'],
    'documents' => ['root' => '/var/www/app/storage/documents'],
]);

$manager->setDefault('avatars');

Registering a disk named default replaces the built-in one. The playground application does exactly this from a config file:

// config/services.php
->set(DiskManager::class, function (App $app) {
    $manager = new DiskManager();
    $disksConfig = require $app->baseDir . '/config/disks.php';

    if (is_array($disksConfig) && !empty($disksConfig)) {
        $manager->registerMultiple($disksConfig);
    }

    return $manager;
})

Access a disk by name — an unknown name throws \InvalidArgumentException:

$disk = $manager->disk('avatars');

$disk->root();   // '/var/www/app/storage/avatars' — trailing slash trimmed
$disk->url();    // 'https://example.com/storage/avatars'
$disk->name();   // 'avatars'
$disk->config(); // ['name' => …, 'root' => …, 'url' => …] plus any extra keys you registered

Register a single custom Disk:

$manager->register(new Disk(
    name: 'thumbnails',
    root: '/var/www/app/storage/thumbnails',
    url:  'https://example.com/storage/thumbnails',
));

Check and list disks:

$manager->has('avatars');    // bool
$manager->all();             // DiskInterface[] keyed by name, 'default' included
$manager->getDefault();      // DiskInterface

DiskManager::createDisk($name, $config) builds a Disk from the same array shape without registering it.

Storage and StorageInterface

Disks say where the originals are. Spline\Image\StorageInterface says where the generated variants go and what URL they are served from. Storage is the default implementation:

use Spline\Image\Storage;

$storage = new Storage(
    baseMediaRoot: '/var/www/app/public/media',   // default '/media'
    baseMediaUrl:  '/media',                       // default '/media'
    uploadsDir:    '/var/www/app/uploads',         // default '/uploads'
);

Trailing slashes are trimmed. For a file it resolves:

$storage->mediaRoot($file);
// '/var/www/app/public/media/images/{disk}/{hash}/photo.jpg'
$storage->mediaUrl($file);
// '/media/images/{disk}/{hash}/photo.jpg'

{hash} is the first ten characters of md5() of the path relative to uploadsDir, followed by - and the original's modification time. Relative, so a deploy to a different directory does not change every URL; with the mtime, so rewriting an original in place changes its URL and a CDN cannot keep serving the old bytes. Variants can therefore be cached for a year. uploadsDir() is what File::relativePathFromUploads() strips off; a file outside it falls back to its bare filename.

The playground wires it from a config file and injects the interface, never the class:

// config/services.php
->set(StorageInterface::class, function (App $app) {
    $config = require $app->baseDir . '/config/storage.php';

    return new Storage(
        baseMediaRoot: $config['baseMediaRoot'] ?? '/media',
        baseMediaUrl:  $config['baseMediaUrl'] ?? '/media',
        uploadsDir:    $config['uploadsDir'] ?? '/uploads',
    );
})

Implement StorageInterface yourself when variants live on another host or under a different layout; PhotoLab, File and the transformations only ever talk to the interface.

Variants: PhotoLab, ImageProcessor and transformations

Resizing on upload is fine for one avatar. For a gallery that needs six sizes per image it is not, so Spline Image separates describing a variant from generating it:

  1. PhotoLab / ImageProcessor work out the variant's filename and URL from the requested transformations. They never touch pixels.
  2. If that file does not exist yet, they record a job — the original's path and the options — through a JobStorageInterface.
  3. A route serving the media URL finds no file, loads the job, runs a Darkroom and saves the result. Every later request is a static file.

Spline\Image\PhotoLab is the entry point. It needs the absolute path of an original, its disk name, a StorageInterface, a JobStorageInterface and optionally the DiskManager that resolves the name:

use Spline\Image\JsonJobStorage;
use Spline\Image\PhotoLab;

$lab = new PhotoLab(
    absolutePath: '/var/www/app/uploads/photo.png',
    disk:         'default',
    storage:      $storage,
    jobStorage:   new JsonJobStorage(),
    diskManager:  $diskManager,
);

$variant = $lab->resize(300, 200, 80);   // ImageVariant
$variant->filename();                    // 'photo-300x200-q80.png'
$variant->url();                         // '/media/images/default/{hash}/photo-300x200-q80.png'
$variant->root();                        // where the generated file will be
$variant->exists();                      // false until something generates it
$variant->modifications();               // ['width' => 300, 'height' => 200, 'quality' => 80]
$variant->original();                    // the FileInterface it was made from

A missing path throws \InvalidArgumentException from the constructor. The playground hides the wiring behind a small factory service so controllers only pass the path:

// src/Service/ImageService.php (playground)
public function make(string $absolutePath): PhotoLab
{
    return new PhotoLab($absolutePath, $this->disk, $this->storage, $this->jobStorage, $this->diskManager);
}

Shorthands, each returning ImageVariant for a resizable image:

$lab->resize(?int $width, ?int $height, ?int $quality);
$lab->crop(int $width, ?int $height, string $mode = 'center');   // 'photo-100x100-crop.png'
$lab->quality(int $level);                                       // 'photo-q70.png'
$lab->blur(int|bool $intensity = true);                          // 'photo-blur10.png'; true → 10
$lab->sharpen(int $amount = 50);                                 // 'photo-sharpen50.png'
$lab->grayscale();   // also bw() and greyscale()                // 'photo-bw.png'

For a file that is not a resizable image — a PDF, a text file — every shorthand returns the FileInterface itself instead of a variant, so callers can always ask for ->url().

srcset() builds the value of an <img srcset> attribute, one resized variant per width:

$lab->srcset([480, 960, 1440]);
// '/media/…/photo-480x.png 480w, /media/…/photo-960x.png 960w, /media/…/photo-1440x.png 1440w'

$lab->srcset([800 => '1x', 1600 => '2x']);   // choose each descriptor yourself
$lab->srcset([]);                            // null — nothing to list

A list of widths describes each variant by its width. A map of width to descriptor takes a width descriptor such as '480w' or a pixel density such as '2x' or '1.5x'. Anything else, or a width below 1, throws \InvalidArgumentException. For a file that cannot be resized, srcset() returns null rather than listing the original under several widths.

Building a pipeline

build() returns a Spline\Image\ImageProcessor you can stack transformations on. Each transformation implements Transformation — name(), config() and apply() — and lives under Spline\Image\Transformations:

use Spline\Image\Transformations\CropTransformation;
use Spline\Image\Transformations\QualityTransformation;

$variant = $lab->build()
    ->add(new CropTransformation(300, 200, 'top left'))
    ->add(new QualityTransformation(80))
    ->process();
Class Constructor config() keys
ResizeTransformation (?int $width, ?int $height, ?int $quality) width, height, quality — only those set
CropTransformation (int $width, ?int $height = null, string $mode = 'center') — a null height becomes $width width, height, crop
QualityTransformation (int $quality = 90) quality
BlurTransformation (int $intensity = 10) blur
SharpenTransformation (int $amount = 50) sharpen
GrayscaleTransformation () grayscale (always true)

config() keys are the Darkroom options by name: the merged array is what the filename tokens are derived from and what the stored job replays, and a key the Darkroom does not know is silently ignored. A custom transformation should therefore emit those keys too.

process() merges every config() into one options array, derives the filename from it, saves a job if the file is missing and returns the ImageVariant. It throws ImageException if the original has vanished or is unreadable since the PhotoLab was built, or if the job cannot be saved. With no transformations added it describes the original served from the media path. getTransformationNames(), getConfigurations() and clear() let you inspect or reset the stack; the tests cover all three.

Variant filenames — CustomFilename

Spline\Image\CustomFilename turns an original name, a template and an options array into the variant name. The tokens, in order, each omitted when unset:

Option Token Example
width / height {w}x{h} — either side may be empty 300x200, 100x
crop crop, or crop-{position} when not centred — true is centred, as in the Darkroom crop, crop-top-left
blur blur{n} — true means 10, as in the Darkroom blur10
grayscale / greyscale / bw bw bw
sharpen sharpen{n} — true means 50, as in the Darkroom sharpen50
quality q{n} q80
use Spline\Image\CustomFilename;

$filename = new CustomFilename('Some File.jpg', '{{ name }}{{ attributes }}.{{ extension }}', [
    'width' => 300, 'height' => 200, 'crop' => 'center', 'blur' => 10, 'grayscale' => true, 'sharpen' => 50, 'quality' => 80,
]);

(string) $filename;      // 'some-file-300x200-crop-blur10-bw-sharpen50-q80.jpg'

$filename->name();       // 'some-file'
$filename->extension();  // 'jpg' — the `format` option, else the original's; `jpeg` is spelled `jpg`
$filename->tokens();     // ['300x200', 'crop', 'blur10', 'bw', 'sharpen50', 'q80']

The name is slugged, the extension lowercased with jpeg folded to jpg, and a format option replaces the extension. Any .. (plain or URL-encoded) in the filename or template throws ImageException::pathTraversalAttempt().

Job storage

A job is the recipe for one variant: which original, which options. Spline\Image\JobStorageInterface has four methods, all keyed by the variant's media directory and filename:

saveJob(string $mediaRoot, string $thumbName, array $options): void
loadJob(string $mediaRoot, string $thumbName): ?array
deleteJob(string $mediaRoot, string $thumbName): bool
jobExists(string $mediaRoot, string $thumbName): bool

ImageProcessor stores the options plus filename (the original, relative to uploadsDir) and transformations (the names in order).

JsonJobStorage writes one JSON file per job under a subdirectory of the media root — .jobs by default, <thumbName>.json inside it. Path components in the thumb name are stripped. Each job is written to a temporary file and renamed into place, so a request reading it at the same moment never sees half a job. A job already stored under that name is left alone: a variant's name follows from its options and source, so a render that describes a pending variant again does not rewrite its file. A job that cannot be written throws ImageException: without its job a variant can never be generated, and its URL would answer 404.

use Spline\Image\JsonJobStorage;

$jobs = new JsonJobStorage();            // or new JsonJobStorage('queue')

$jobs->saveJob('/var/www/app/public/media/images/default/abc123-1700000000', 'photo-300x200.png', [
    'width' => 300, 'height' => 200,
]);
// writes …/abc123-1700000000/.jobs/photo-300x200.png.json

$jobs->jobExists($mediaRoot, 'photo-300x200.png');   // true
$jobs->loadJob($mediaRoot, 'photo-300x200.png');     // ['width' => 300, 'height' => 200]; null when missing or not JSON
$jobs->deleteJob($mediaRoot, 'photo-300x200.png');   // true; false when there was nothing to delete

ImageJobService is the database-backed alternative. It adapts an ImageJobRepositoryInterface — the same four methods, implemented by your application's ORM repository — to JobStorageInterface, and rethrows a failed saveJob() as ImageException for the same reason. The reads fail soft: loadJob() returns null, and deleteJob() and jobExists() return false:

use Spline\Image\ImageJobService;

$jobs = new ImageJobService($imageJobRepository);   // your ImageJobRepositoryInterface

Which one to use is a service definition. The playground binds a Doctrine implementation directly:

// config/services.php
->set(JobStorageInterface::class, fn (App $app) => new DoctrineJobStorage(
    $app->get(ImageJobRepository::class),
))

Serving and generating variants: VariantGenerator

A variant URL points at a file that does not exist until it is first requested. Spline\Image\VariantGenerator is what the route behind baseMediaUrl() calls to answer it. Give it the three path segments after /images/. It returns the variant's absolute path, generating the file from its job first when it is missing:

use Spline\Image\Darkroom\GdLib;
use Spline\Image\VariantGenerator;

#[Route('/media/images/{disk}/{hash}/{filename}', name: 'media.variant', methods: ['GET'])]
public function variant(string $disk, string $hash, string $filename): ResponseInterface
{
    $generator = new VariantGenerator($this->storage, $this->jobStorage, new GdLib(), $this->diskManager);
    $path = $generator->generate($disk, $hash, $filename);

    if (null === $path) {
        return new Response(404);
    }

    return new Response(200, [
        'Content-Type' => mime_content_type($path) ?: 'application/octet-stream',
        'Cache-Control' => 'public, max-age=31536000, immutable',
    ], Stream::create(fopen($path, 'rb')));
}

What it does for you:

  • Only described variants are generated. The URL names a file; the size, crop and quality come from its job. Nothing in a request can ask for a size nobody described.
  • Path segments are checked. An empty segment, ., .., a slash, a backslash or a null byte returns null. With a DiskManager, a disk it does not know returns null too.
  • The original must be inside the uploads directory. The job's filename is resolved with realpath() and compared as a whole directory, so ../uploads-old/x.jpg does not pass for uploads/.
  • Writes are atomic. The original is copied to a temporary file beside the variant, processed and renamed into place. A reader never sees half a file, and two requests for the same variant do not write over each other.
  • The job is kept. It is the recipe: a purged variant is rebuilt on its next request instead of answering 404.

generate() throws ImageException when the darkroom refuses or fails, for example an original over the pixel limit. Answer that with a 500, or a 422 if you prefer, and log it.

Once a variant exists, let the web server serve it directly and fall through to this route only for missing files. With Apache or nginx that is a try_files-style rule on /media/images/.

Templates: Pictures and Picture

Templates rarely want a PhotoLab. They want a URL at a size, a srcset, an aspect ratio for the layout box and a focus for object-position. Spline\Image\Pictures makes Picture objects that give exactly that. Register one Pictures in the container:

use Spline\Image\Pictures;

$pictures = new Pictures($storage, $jobStorage, $diskManager);   // disk 'default'
$photos = $pictures->withDisk('photos');                         // a copy; $pictures is unchanged

$picture = $photos->of($absolutePath, focus: '30% 60%');

withDisk() returns a new Pictures, so a shared instance never changes disk under another caller.

Every size change returns a new Picture, leaving the one you called it on as it was:

$picture->resize(1500)->url();            // fit within 1500px wide, never enlarged
$picture->crop(600, 400)->url();          // exactly 600 × 400, the focus kept in frame
$picture->crop(600)->url();               // a 600px square
$picture->url();                          // the original, served from the media path

$picture->srcset([500, 800, 1200]);       // '… 500w, … 800w, … 1200w'
$picture->crop(600, 400)->srcset([300, 600, 900]);   // every width cropped 3:2

$picture->width();                        // 4000 — the size url() serves
$picture->crop(600, 400)->ratio();        // 1.5
$picture->focus();                        // '30% 60%', ready for object-position
$picture->original()->exif()->camera();   // the original Image, for its metadata

In a template:

<?php $thumb = $picture->crop(800, 600) ?>
<img src="<?= $thumb->url() ?>"
     srcset="<?= $thumb->srcset([400, 800, 1200]) ?>"
     sizes="(max-width: 800px) 100vw, 800px"
     width="<?= $thumb->width() ?>" height="<?= $thumb->height() ?>"
     style="object-position: <?= $picture->focus() ?>"
     alt="">

width(), height() and ratio() report what url() will serve, worked out the same way the darkroom plans it, so width and height attributes reserve the right space before the image loads. A photo stored rotated reports its upright size.

focus accepts a focal point ('30% 60%', '30%, 60%'), a named position ('top left', see Focus), JSON from the former Focus plugin, or null for the center. A crop passes it to the darkroom as given, so its variant URLs stay stable. focus() always returns the clean 'x% y%' form.

For a file that cannot be resized, such as a PDF or an SVG, url() and srcset() return null and (string) $picture is ''. Serve those through your own route; isResizable() and isSvg() tell you which case you are in. Picture has no method for inlining SVG markup on purpose: an uploaded SVG can carry script.

ImageException

Spline\Image\ImageException extends \RuntimeException and is what the image classes throw. Named constructors give each failure a stable message:

ImageException::fileNotFound($path);
ImageException::fileNotReadable($path);
ImageException::invalidImageType($path, $type);
ImageException::mimeTypeMismatch($path, $extension, $mime);
ImageException::transformationFailed($message, $previous);
ImageException::pathTraversalAttempt($value);
ImageException::tooManyPixels($path, $width, $height, $maxPixels);
ImageException::jobNotSaved($thumbName, $reason, $previous);

PhotoLab's constructor and DiskManager::disk() throw plain \InvalidArgumentException instead; the Darkroom drivers throw \RuntimeException for a missing extension, package or binary.

Full upload and resize workflow

In an application on spline/framework, which provides Upload and Str::uuid(): store the original under a name that cannot collide — saveTo() refuses to overwrite — then make the thumbnail from a copy, because process() writes in place.

use Spline\Framework\Http\Upload;
use Spline\Framework\Toolkit\Str;
use Spline\Image\Darkroom\GdLib;

#[Route(path: '/profile/avatar', name: 'profile.avatar.update', methods: ['POST'])]
public function updateAvatar(ServerRequestInterface $request, #[CurrentUser] User $user): ResponseInterface
{
    $upload = Upload::from($request->getUploadedFiles()['avatar'])
        ->isImage()
        ->maxSize(5 * 1024 * 1024);

    if ($upload->hasErrors()) {
        return Response::json(['errors' => $upload->getErrors()], 422);
    }

    // Keep the extension — saveTo() uses the filename verbatim, and the
    // thumbnail is written in the same format. A UUID means no two uploads collide.
    $ext = strtolower(pathinfo($upload->getFile()->getClientFilename(), PATHINFO_EXTENSION));
    $upload->saveTo($this->storageDir . '/avatars', Str::uuid() . '.' . $ext);

    $original = $upload->getStoredFilePath();
    $thumb = dirname($original) . '/' . pathinfo($original, PATHINFO_FILENAME) . '-256.' . $ext;

    // process() overwrites its input, so work on a copy
    copy($original, $thumb);
    (new GdLib())->process($thumb, [
        'width'  => 256,
        'height' => 256,
        'crop'   => true,
    ]);

    $user->setAvatarPath($thumb);
    $this->entityManager->flush();

    return Response::redirect($this->urlGenerator->generate('profile'));
}

For anything beyond a single fixed size, hand the stored original to Pictures instead and let the media route generate variants on demand with VariantGenerator.

Reading dimensions before processing

Check dimensions before committing to a full resize:

$dims = Dimensions::forImage($upload->getStoredFilePath());

if ($dims->width() < 100 || $dims->height() < 100) {
    return Response::json(['error' => 'Image must be at least 100×100 pixels.'], 422);
}