Download the PHP package tihloh/prefab-image without Composer
On this page you can find all versions of the php package tihloh/prefab-image. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tihloh/prefab-image
More information about tihloh/prefab-image
Files in tihloh/prefab-image
Package prefab-image
Short Description Fast framework-independent image inspection, resizing, conversion, optimization and delivery for Prefab PHP.
License MIT
Homepage https://github.com/tihloh/prefab-php
Informations about the package prefab-image
Prefab Image
Prefab Image provides fast, framework-independent image inspection, resizing, cropping, conversion and browser delivery for PHP applications.
Process once. Store optimized variants. Serve static files directly whenever possible.
The package uses PHP GD directly and keeps the hot path small: no ORM, service container, reflection or automatic database work.
Requirements
- PHP 8.1 or newer
ext-gdext-exifis optional for JPEG auto-orientation
Installation
When published:
Quick start
resize(1200) preserves the aspect ratio and does not upscale by default.
Resize inside a box
The image fits inside 1200x800 while preserving its aspect ratio.
Cover / thumbnail
Shortcut:
cover() scales and center-crops to fill the requested dimensions. It does not upscale unless explicitly requested.
Crop, rotate and flip
Flip directions:
Auto orientation
When ext-exif is available, JPEG EXIF orientation is applied. Without EXIF support the call is a no-op.
Formats and quality
Supported formats depend on the installed GD build:
Prefab checks encoder support before using optional formats such as WebP and AVIF.
Save, encode or display
Save to a file:
Return encoded binary data:
Display the PHP response directly as an image:
Then the PHP endpoint can be used directly:
display() emits the image body and the correct Content-Type. It does not call exit, so request termination remains under application control.
Browser cache headers
For versioned image URLs:
Cheap image inspection
When only dimensions or MIME information are needed, do not decode the full bitmap:
This is useful for generating correct HTML dimensions:
Cached derivatives
For frequently requested thumbnails and resized images, avoid decoding and processing the source on every request.
If the target exists and is at least as new as the source, generate() returns immediately before decoding the source image.
This is the preferred pattern for dynamic image endpoints that also maintain a disk cache.
Fast delivery strategy
For public images, the fastest normal path is:
Avoid this for every page load when the result can be cached:
Use PHP processing when generating a derivative, serving protected images or producing a one-off response. Reuse the generated static derivative afterward whenever possible.
Responsive variants
A practical upload workflow can generate several widths once:
Then let the browser choose the smallest useful file:
Performance design
The processing object is deliberately mutable. Fluent calls such as quality() and format() do not clone the decoded bitmap. Transformations replace the current GD image resource and release the previous one as soon as it is no longer needed.
Key rules:
API quick reference
| API | Purpose |
|---|---|
Image::open() |
Decode an image file |
Image::fromString() |
Decode image bytes |
Image::inspect() |
Read dimensions/MIME without full processing |
Image::generate() |
Generate a derivative only when stale/missing |
resize() |
Fit while preserving aspect ratio |
cover() / thumbnail() |
Fill and center-crop |
crop() |
Crop a specific rectangle |
rotate() |
Rotate image |
flip() |
Flip horizontally/vertically |
autoOrient() |
Apply JPEG EXIF orientation when available |
format() |
Choose output format |
quality() |
Choose output quality |
save() |
Atomically save to disk |
encode() |
Return encoded bytes |
display() |
Emit image HTTP response |
width() / height() / ratio() |
Read current dimensions |
mime() |
Read current output MIME |
Scope
Prefab Image stays focused on image processing and delivery. It does not attempt to become a graphics editor.
Out of scope for the small core package:
Those capabilities can be separate packages or adapters when an application needs them.
All versions of prefab-image with dependencies
ext-gd Version *