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 Manipulation Quick Reference

This reference lists all methods available in Trongate's Image module when calling $this->image->method_name(). Use this page as your quick lookup guide when working with images.

Note: All image operations include automatic security validation during upload to prevent malicious file uploads. The Image module automatically preserves transparency for PNG and GIF images through all operations.

Getting Images Into Memory

Method Parameters Returns Description
array $config array Upload a NEW image with automatic security validation, resizing, and thumbnail generation.
string $filename void Load an EXISTING image from disk into memory for manipulation.

When to Use Which:

  • upload() - For NEW images from user file uploads. Handles validation, security, and optional automatic resizing/thumbnails.
  • load() - For EXISTING images already on disk. Perfect for batch processing, generating new sizes, or creating variations.

Upload Configuration

Configuration Array Keys

Key Type Required Default Description
destination string yes - Target directory path
upload_to_module bool no false Upload to module directory
target_module string no current Target module name
make_rand_name bool no false Generate random filename
max_width int no 450 Maximum width (0 = no limit)
max_height int no 450 Maximum height (0 = no limit)
thumbnail_dir string no '' Thumbnail directory
thumbnail_max_width int no 0 Thumbnail width
thumbnail_max_height int no 0 Thumbnail height

Upload Example

Load Example

Image Information

Method Parameters Returns Description
none int Get width in pixels of currently loaded image.
none int Get height in pixels of currently loaded image.
none string Get MIME type for currently loaded image (e.g., 'image/jpeg').

Example Usage

Image Resizing

Method Parameters Returns Description
int $width void Resize to specific width, height adjusts proportionally.
int $height void Resize to specific height, width adjusts proportionally.
float $percentage void Scale by percentage (50 = half size, 150 = 1.5× size).

Resize Examples

Image Cropping

Method Parameters Returns Description
int $width
int $height
string $position = 'center'
void Crop to exact dimensions. Position: 'left', 'center', 'right'.
int $width
int $height
void Resize then crop to fill exact dimensions perfectly.

Crop Examples

Image Saving and Output

Method Parameters Returns Description
string $filename = null
int $compression = 100
int $permissions = null
void Save image to disk with optional compression and permissions.
bool $return = false string|null Output image to browser or return as string.
none void Free memory allocated to the image resource.

Save and Output Examples

Common Patterns

Pattern 1: User Avatar Upload with Multiple Sizes

Pattern 2: Generate Multiple Product Image Sizes

Pattern 3: Dynamic Image Serving

Pattern 4: Format Conversion

Pattern 5: Conditional Processing Based on Dimensions

Validation Rules for Image Uploads

Use these validation rules with $this->validation->set_rules():

Rule Parameter Description
required none Image upload is mandatory
max_size KB Maximum file size (e.g., max_size[5000] = 5MB)
max_width pixels Maximum image width
max_height pixels Maximum image height
min_width pixels Minimum image width
min_height pixels Minimum image height

Validation Example

Quality Settings Reference

Quality File Size Visual Quality Best For
100 Largest Maximum Archival, printing, source files
90-95 Large Excellent Hero images, featured content
85-90 Medium Very good Standard web images (recommended)
75-85 Small Good Thumbnails, list views
60-75 Very small Acceptable Previews, backgrounds
<60 Tiny Poor Avoid for web use

Supported Image Formats

Format Extension MIME Type Transparency Compression
JPEG .jpg, .jpeg image/jpeg No Lossy (adjustable 0-100)
PNG .png image/png Yes (alpha channel) Lossless
GIF .gif image/gif Yes (binary) Lossless (256 colors)
WEBP .webp image/webp Yes (alpha channel) Lossy or lossless

File Permission Reference

Permission Octal Description Use Case
rw-r--r-- 0644 Owner writes, all read Public web images (recommended)
rw-r----- 0640 Owner writes, group reads Semi-private images
rw------- 0600 Owner only Private images, documents
rw-rw-r-- 0664 Owner/group write, all read Shared editing environments

Best Practices Summary

Image Manipulation Essentials:

  • ✅ Use for NEW images from users
  • ✅ Use for EXISTING images on disk
  • ✅ Always validate images before uploading (type, size, dimensions)
  • ✅ Use make_rand_name for user-uploaded images (security)
  • ✅ Store complete paths from upload() in your database
  • ✅ Generate common sizes during upload, not on-demand
  • ✅ Reload source with load() when generating multiple sizes
  • ✅ Use quality 85-90 for web images (balance of quality/size)
  • ✅ Set appropriate permissions (0644 public, 0600 private)
  • ✅ Check file existence before load() operations
  • ✅ Preserve originals and generate variants as needed
  • ✅ Use resize_to_width() for most layouts
  • ✅ Use resize_and_crop() for thumbnails and grids
  • ✅ Add caching headers when serving dynamic images
  • ✅ Call destroy() when processing many images in a loop
  • ✅ Use output() for watermarks and on-the-fly generation

Quick Decision Matrix

Task Method(s) Notes
Upload new image upload() Handles validation, security, optional resize/thumbnails
Load existing image load() For batch processing or generating new sizes
Create square thumbnail resize_and_crop() Perfect for avatars and icons
Resize for web display resize_to_width() Most common for responsive layouts
Reduce file size save() with quality 80-85 Good balance for web
Dynamic watermark output() Don't store watermarked versions
Convert format save() with new extension Change .jpg to .webp, etc.
Multiple sizes Loop with load() + resize + save() Reload source each time
Check dimensions first get_width()/get_height() For conditional processing

Need More Details? Each method is fully documented in the Image Module Reference with complete parameter descriptions, return values, and exception handling.