The Image Module

The Image Module provides a robust set of tools for managing file operations within the Trongate framework. It supports essential functionalities such as secure file uploading with automatic MIME validation, directory creation, reading and writing file contents, and file transfers (downloads).

Security is a core feature of this module, with built-in protections against directory traversal attacks and restricted access to sensitive application directories like "config" and "engine". It also includes utilities for managing memory requirements and ensuring unique file naming during uploads.

Comprehensive guidance for this module can be found in the File Management chapter of the Trongate PHP framework documentation.

crop()

Crops a loaded image to the specified dimensions. The method extracts a portion of the current image based on the provided width, height, and trim position, then replaces the loaded image with the cropped version.

destroy()

Frees memory allocated to the loaded image resource. This method releases the GD image resource from memory and should be called when image processing is complete, especially when processing multiple or large images in a single request.

get_header()

Returns the MIME type of the currently loaded image. This method determines the appropriate Content-Type header value for serving images directly to browsers, returning values like 'image/jpeg', 'image/png', 'image/gif', or 'image/webp' based on the loaded image format.

get_height()

Returns the height in pixels of the currently loaded image. This method uses PHP's GD library imagesy() function to retrieve the height of the image resource.

get_width()

Returns the width in pixels of the currently loaded image. This method uses PHP's GD library imagesx() function to retrieve the width of the image resource.

load()

Loads an existing image file into memory for processing. This method performs comprehensive security validation before loading, including MIME type verification, file signature checking, malicious content scanning, and memory requirement calculations. Once loaded, the image is ready for manipulation operations such as resizing, cropping, or scaling.

output()

Outputs the currently loaded image directly to the browser or returns the image data as a string. This method provides two modes of operation: streaming the image directly to the browser for immediate display, or capturing the image data as a binary string for further processing or storage.

resize_and_crop()

Resizes and crops a loaded image to exact dimensions in a single operation. This method intelligently determines the optimal resize strategy based on aspect ratios, then crops to achieve the target dimensions precisely. The result is an image that exactly matches the specified width and height with no distortion.

resize_to_height()

Resizes a loaded image to the specified height whilst automatically calculating and maintaining the correct width to preserve the original aspect ratio. This method ensures proportional scaling with no distortion.

resize_to_width()

Resizes a loaded image to the specified width whilst automatically calculating and maintaining the correct height to preserve the original aspect ratio. This method ensures proportional scaling with no distortion.

save()

Saves the currently loaded and processed image to disk. This method writes the image resource to a file with optional compression settings for JPEG and WEBP formats, and allows setting file permissions. The method automatically handles format-specific requirements such as transparency preservation for PNG images.

scale()

Scales a loaded image by a percentage, adjusting both width and height proportionally to maintain the aspect ratio. This method provides an intuitive way to resize images relative to their current size rather than specifying absolute dimensions.

upload()

Handles the complete image upload process including validation, file storage, automatic resizing, and thumbnail generation. This method manages the entire upload workflow from receiving the uploaded file through to saving processed versions with proper security validation.