Image Utilities

Lightpack’s Image utility provides a robust, chainable API for image manipulation, supporting all common operations for avatars, thumbnails, and web graphics. It is built on PHP’s GD extension and covers resizing, cropping, filters, format conversion, and more, with strong error handling and test coverage.


Overview


Basic Usage

use Lightpack\Utils\Image;

// Load image
$image = new Image('/path/to/photo.jpg');

// Resize and save
$image->resize(400, 300)->save('/path/to/output.jpg');

// Crop, grayscale, and save as PNG
$image = new Image('/path/to/photo.jpg');
$image->crop(100, 100, 10, 10)
      ->grayscale()
      ->save('/path/to/cropped.png');

Loading Images

$image = new Image('photo.jpg');
$image = new Image('https://example.com/pic.png');

Resizing & Cropping

// Resize (maintains aspect ratio if one dimension is zero)
$image->resize(300, 0); // 300px wide, proportional height

// Crop (x, y, width, height)
$image->crop(100, 100, 10, 10);

Saving & Format Conversion

The save() method writes the current image to disk, automatically selecting the format (JPEG, PNG, or WebP) based on the file extension you provide. It also lets you control quality and compression for each format.

// Save as JPEG (with quality)
$image->save('output.jpg', 90);    // 90% quality (default is 80)

// Save as PNG (with compression)
$image->save('output.png');        // Compression defaults to 7 (0 = none, 9 = max)
$image->setDefaultPngCompression(4); // Set default for future PNG saves

// Save as WebP (with quality)
$image->save('output.webp', 80);   // 80% quality (default is 80)

Format detection:

Quality & Compression:

Directory checks:

Overwriting:

Image object state after save:

$image = new Image('photo.jpg');
$image->resize(200, 200)->save('small.jpg');

// Need to reload or re-instantiate for further use:
$image = new Image('photo.jpg');
$image->resize(400, 400)->save('large.jpg');

Tip: If you want to save multiple versions (e.g., different sizes or formats), clone the Image object before calling save():

$img = new Image('photo.jpg');
$clone = clone $img;
$img->resize(200, 200)->save('small.jpg');
$clone->resize(400, 400)->save('large.jpg');

Best Practices:

Warning: If you try to use the same Image object after save(), you will get an error because the underlying image resource has been destroyed.


Quality & Compression Settings

$image->setDefaultJpegQuality(85);
$image->setDefaultWebpQuality(90);
$image->setDefaultPngCompression(6); // 0 (none) to 9 (max)

Avatars & Thumbnails

Lightpack’s Image utility provides dedicated methods for generating standardized avatar and thumbnail images—ideal for user profiles, listings, galleries, blog posts, and more. These helpers automate resizing and format selection, ensuring consistency and optimal display across your application.

What Are Avatars and Thumbnails?

Standard Sizes

Note: Thumbnail height is automatically calculated to preserve the original aspect ratio.

Generating Avatars

$image = new Image('userpic.jpg');
$paths = $image->avatar('avatars/user123');
// $paths['small']  => 'avatars/user123_avatar_small.webp'
// $paths['medium'] => 'avatars/user123_avatar_medium.webp'
// $paths['large']  => 'avatars/user123_avatar_large.webp'
$paths = $image->avatar('avatars/user123', ['small', 'large']);

Generating Thumbnails

$image = new Image('photo.jpg');
$thumbs = $image->thumbnail('thumbs/photo123');
// $thumbs['small']  => 'thumbs/photo123_thumb_small.jpg'
// $thumbs['medium'] => 'thumbs/photo123_thumb_medium.jpg'
// $thumbs['large']  => 'thumbs/photo123_thumb_large.jpg'
$thumbs = $image->thumbnail('thumbs/photo123', ['medium']);

File Naming Conventions

Custom Sizes & Error Handling

Method Chaining & Reuse

Best Practices

Tip: You can combine avatars, thumbnails, and other manipulations by chaining methods or by working with clones for batch processing.


Filters & Effects

$image->grayscale()->contrast(-20)->brightness(30)->blur(2)->sharpen();
$image->colorize(255, 0, 0)->sepia()->invert()->pixelate(8);
$image->emboss()->edgedetect()->posterize();

Watermark & Text Overlay

// Overlay PNG watermark at (x, y), opacity 0-100
$image->watermark('logo.png', 10, 10, 60);

// Overlay text (x, y, size, color, font)
$image->text('Sample', 20, 40, 16, '#ff0000', '/path/to/font.ttf');

Rotating & Flipping

$image->rotate(90); // Degrees, counter-clockwise
$image->flip('vertical'); // or 'horizontal'

Error Handling & Edge Cases