How to Master On‑Demand Image Manipulation in PHP with Glide

This guide introduces the open‑source PHP library Glide, explains its core features such as on‑the‑fly image processing, caching, and security, and provides step‑by‑step instructions for installation, configuration, basic usage, common manipulators, and a real‑world e‑commerce example.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Master On‑Demand Image Manipulation in PHP with Glide

Overview

Glide is a PHP image‑processing library maintained by The PHP League that offers on‑demand image manipulation via an HTTP API, integrates with local filesystems, and runs entirely within your PHP environment.

What Is Glide?

Glide builds on Intervention Image for core image operations and uses Flysystem to abstract the filesystem, supporting storage back‑ends like local disks and S3. Key features include:

On‑Demand Processing : No pre‑generated thumbnails; images are resized or transformed through URL parameters.

Automatic Caching : Processed images are cached with long‑lived expiration headers for better performance.

Flexible Integration : Can run as a standalone image server or be embedded directly in an application.

Multi‑Driver Support : Works with GD, Imagick, and libvips extensions.

Security : HTTP signature protection prevents URL abuse.

Framework‑Agnostic : PSR‑2 compliant and usable in any PHP project.

Installation

Install Glide via Composer (requires PHP ≥ 8.1 and GD or Imagick): composer require league/glide Verify the installation:

composer show league/glide

Configuration

The core of Glide is the Server object, which manages source files, cache, and manipulators. A quick configuration using the factory method looks like this:

<?php
require 'vendor/autoload.php';
use League\Glide\ServerFactory;
$server = ServerFactory::create([
    'source' => new League\Flysystem\Filesystem(new League\Flysystem\Local\LocalFilesystemAdapter(__DIR__ . '/images/source')),
    'source_path_prefix' => 'images/',
    'cache' => new League\Flysystem\Filesystem(new League\Flysystem\Local\LocalFilesystemAdapter(__DIR__ . '/images/cache')),
    'cache_path_prefix' => 'cache/',
    'base_url' => 'http://yourapp.com/images/',
    'driver' => 'gd', // or 'imagick'
    'max_image_size' => 2000 * 2000,
]);
echo "Glide Server configuration complete!";

This sets the source directory, cache directory, base URL, image driver, and maximum image size.

For advanced use‑cases you can manually instantiate the server, image manager, and a list of manipulators (e.g., orientation, crop, encode). The Encode manipulator is required to produce the final output.

<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
use League\Glide\Api\Api;
use League\Glide\Manipulators\Orientation;
use League\Glide\Manipulators\Crop;
use League\Glide\Manipulators\Encode;
use League\Glide\Server;

$source = new Filesystem(new LocalFilesystemAdapter(__DIR__ . '/images/source'));
$cache = new Filesystem(new LocalFilesystemAdapter(__DIR__ . '/images/cache'));
$imageManager = new ImageManager(new Driver());
$manipulators = [new Orientation(), new Crop(), new Encode()];
$api = new Api($imageManager, $manipulators);
$server = new Server($source, $cache, $api);

Basic Usage

Glide’s API works through query parameters. Assuming a source image kayaks.jpg and a base URL http://yourapp.com/images/, a simple request handler image.php can be written as:

<?php
require 'vendor/autoload.php';
// $server configuration as shown above
$path = $_SERVER['REQUEST_URI']; // e.g. /kayaks.jpg?w=800&h=600
$modifiers = $_GET; // ['w' => 800, 'h' => 600]
$image = $server->getImageResponse($path, $modifiers);
$image->sendContent();

In HTML you would use:

<img src="http://yourapp.com/image.php/kayaks.jpg?w=800&fit=crop" alt="Resized Image">

This resizes the original image to 800 px width and crops it to fit.

Common Manipulators

Width (w) : Set image width in pixels, e.g., ?w=800.

Height (h) : Set image height in pixels, e.g., ?h=600.

Fit (fit) : Choose how the image fits the dimensions – crop, contain, stretch, fill.

Brightness (bri) : Adjust brightness from -100 to 100, e.g., ?bri=20.

Contrast (con) : Adjust contrast from -100 to 100, e.g., ?con=30.

Sharpen (sharp) : Sharpen intensity 1‑100, e.g., ?sharp=10.

Blur (blur) : Blur intensity 1‑100, e.g., ?blur=5.

Gamma (gam) : Gamma correction 0.1‑5, e.g., ?gam=1.2.

Watermark (mark) : Add a watermark image, e.g., ?mark=logo.png&markw=100&markpos=center.

Background (bg) : Set background color in HEX, e.g., ?bg=ffffff.

Quality (q) : Output quality 1‑100, e.g., ?q=90.

Format (fm) : Choose output format such as jpg, png, webp, e.g., ?fm=webp.

These can be combined, for example

?w=400&h=300&fit=crop&bri=10&sharp=5&q=85

to crop to 400×300, increase brightness, sharpen, and output at high quality.

Caching and Security

Caching : Glide stores processed images in a cache directory, optionally grouping them in sub‑folders ( 'group_cache_in_folders' => true) and preserving file extensions. Pairing with a CDN further speeds delivery.

Security : Enable signed URLs to prevent abuse:

'security' => new League\Glide\Urls\UrlSigner('your-secret-key'),

Generate a signed URL with $signer->getUrl('path.jpg?mods', $baseUrl).

Real‑World Example

For an e‑commerce site that needs multiple product image sizes, integrate Glide in image.php and use template code such as:

<!-- Thumbnail -->
<img src="image.php/product1.jpg?w=200&h=200&fit=contain&q=80" alt="Thumbnail">

<!-- Detail view -->
<img src="image.php/product1.jpg?w=800&fit=crop&sharp=10&bg=ffffff" alt="Detail">

This eliminates the need to generate hundreds of static images, saving storage and processing time.

Conclusion

Glide provides a lightweight, HTTP‑API‑driven solution for professional‑grade image manipulation in PHP. Refer to the official documentation (https://glide.thephpleague.com/) for advanced topics like presets and response factories. The project repository is https://github.com/thephpleague/glide.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendGlideimage-processingHTTP API
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.