Trongate PHP Framework Docs
Introduction
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Authorization & Authentication
Tips And Best Practices

Image Operations

Once images are on your server, you can manipulate them using the Image module. For example, you can resize product photos for consistency, crop user avatars to perfect squares, generate multiple sizes for responsive layouts, or scale screenshots for documentation.

Trongate's Image module provides direct access to PHP's GD library. Every operation follows the same stateful pattern: load an image, manipulate it, then either save it back to disk or output it directly to the browser.

The Stateful Workflow Pattern

Unlike functional APIs where you pass images between methods, Trongate's Image module maintains state. You load an image once, perform multiple operations on it, then decide what to do with the result.

This pattern ensures you always know what image you're working with and prevents accidental operations on the wrong image.

State Management: The Image module holds one image in memory at a time. Each call to load() replaces the current image. Operations like resize() and crop() modify the loaded image directly.

Loading Images

Before you can manipulate an image, you need to load it into memory using :

The load() method automatically detects the image format (JPEG, PNG, GIF, WEBP) and prepares it for manipulation.

Important: Loading a new image replaces any previously loaded image. If you need to process multiple images, complete all operations on one before loading the next, or use separate Image module instances.

Resizing Images

Resizing is the most common image operation. Trongate provides three methods for different resizing needs, all of which preserve aspect ratios automatically.

Aspect Ratio Preservation: All resizing methods preserve aspect ratios automatically. Unlike some image libraries that require manual calculations or risk image stretching, Trongate handles proportions for you - a 4:3 image remains 4:3, just larger or smaller. You never need to calculate dimensions yourself.

Resize to Width

Use when you know the target width but want the height to adjust proportionally:

Use case: Blog post images, product photos, content images where width matters more than height.

Resize to Height

Use when you need a specific height with proportional width:

Use case: Vertical layouts, sidebar images, thumbnail strips with consistent heights.

Scale by Percentage

Use when you want to resize proportionally by a percentage:

Use case: Creating retina/standard versions, generating preview images, bulk resizing operations.

Performance Tip: Resizing operations are fast but not instant. For user-facing uploads, resize during the upload process using the max_width and max_height configuration options rather than as a separate step.

Cropping Images

Cropping removes portions of an image to focus on specific areas or achieve exact dimensions.

Basic Cropping

Use to extract a specific region:

Crop Positioning

Control which part of the image is retained using the optional third parameter:

Creating Square Avatars

Resize and Crop (Perfect Fitting)

Transparency Support: The Image module preserves transparency for PNG and GIF images automatically through all operations:

  • PNG alpha channels remain intact during resizing, cropping, and scaling
  • GIF transparency colors are preserved
  • When saving to JPEG (which doesn't support transparency), transparent areas become white backgrounds

This happens automatically - no configuration needed. PNGs stay crisp with transparent backgrounds, perfect for logos and overlays.

The method combines resizing and cropping to fill exact dimensions while preserving as much of the original image as possible.

How It Works

The method intelligently determines whether to resize by width or height first:

Use case: Product thumbnails, card layouts, grid galleries where every image must be identical dimensions.

Why Use This? Regular crop() only removes excess pixels. resize_and_crop() first scales the image to ensure the target dimensions are filled, then crops. This prevents blank spaces while maintaining aspect ratios.

Generating Multiple Sizes from One Source

A common pattern is creating several versions of an image for different contexts - hero images, thumbnails, mobile versions. Here's how to do it efficiently:

Practical Example: Blog Post Image Processor

Getting Image Dimensions

Sometimes you need to know an image's size before deciding how to process it:

Real-World Pattern: Responsive Image Generator

Here's a complete example that generates all the sizes needed for modern responsive web design:

Using the Responsive Generator

Operation Reference

Method Parameters Purpose
string $path Load image into memory for manipulation
int $width Resize to specific width, height adjusts proportionally
int $height Resize to specific height, width adjusts proportionally
float $percentage Scale by percentage (50 = half size, 150 = 1.5x size)
int $width, int $height, string $position Crop to exact dimensions ('left', 'center', 'right')
int $width, int $height Fill exact dimensions by resizing then cropping
none Get current image width in pixels
none Get current image height in pixels
string $path, int $quality, int $permissions Save to disk (covered in next page)
bool $return Output to browser (covered in next page)

Image Compression Quality: JPEG and WEBP images accept a quality parameter from 0-100:

  • 100 = maximum quality, largest files (default)
  • 85-90 = recommended for web images (excellent quality, good compression)
  • 70-80 = noticeable quality loss, smaller files (use for thumbnails)
  • Below 70 = significant degradation (avoid for main content)

Note: PNG and GIF use lossless compression, so they don't accept a quality parameter. The quality setting only affects JPEG and WEBP formats.

Image Operation Guidelines:

  • ✅ Always load() before manipulating
  • ✅ Reload source image when generating multiple sizes
  • ✅ Use resize_to_width() for most layouts
  • ✅ Use resize_and_crop() for thumbnails and grids
  • ✅ Check dimensions with get_width()/get_height() for conditional logic
  • ✅ Generate thumbnails during upload, not on-demand
  • ✅ Store all generated paths in your database
  • ✅ Use quality 85-90 for web images (good balance)