Skip to content

[13.x] Adds first-party support for image processing#59276

Draft
nunomaduro wants to merge 10 commits into13.xfrom
feat/image
Draft

[13.x] Adds first-party support for image processing#59276
nunomaduro wants to merge 10 commits into13.xfrom
feat/image

Conversation

@nunomaduro
Copy link
Member

Hey! This one's still cooking — not ready for review yet, just sharing progress. 🚧

This PR adds first-party support for image processing in Laravel via a driver-based API that allows developers to manipulate uploaded images fluently from requests.

Getting Started

Process an uploaded image in a single, fluent chain:

$request->image('avatar')
      ->cover(200, 200)
      ->optimize(format: 'webp') // e.g: 8mb to 56 kb..
      ->store('avatars');

The image() method on the request returns a PendingImage instance that wraps the uploaded file. Operations (like optimize or cover) are accumulated and applied when a terminal method like store() is called — or explicitly via process().

Available Operations

optimize(string $format = 'webp', int $quality = 80)

Convert to a given format and quality level:

$request->image('photo')->optimize(); // defaults 'webp', 85
$request->image('photo')->optimize(format: 'png', quality: 50);
cover(int $width, int $height)

Resize and crop the image to exactly fit the given dimensions, maintaining aspect ratio:

$request->image('avatar')->cover(200, 200);
scale(int $width, int $height)

Resize the image proportionally. If only width is given, the height is calculated automatically:

$request->image('photo')->scale(1200);
orient()

Auto-orient the image based on EXIF data — fixes photos taken on phones that appear rotated:

$request->image('photo')->orient();
blur(int $amount = 5)

Apply a blur effect. Useful for generating placeholder images for lazy loading:

$request->image('hero')->scale(40)->blur(15);
greyscale()

Convert the image to greyscale:

$request->image('photo')->greyscale();

Real-World Pipelines

// Avatar: 8MB DSLR photo → tiny optimized square
$request->image('avatar')
    ->orient()
    ->cover(200, 200)
    ->optimize('webp')
    ->store('avatars');

// Album: large photo → reasonable size, auto-rotated
$request->image('photo')
    ->orient()
    ->scale(1200)
    ->optimize('webp', 85)
    ->store('photos');

// Lazy-load placeholder
$request->image('hero')
    ->scale(40)
    ->blur(15)
    ->optimize('webp', 50)
    ->store('placeholders');
Drivers

The image processing is driver-based. Three drivers are included: gd, imagick, and cloudflare.

The default driver is configured in config/image.php:

'default' => env('IMAGE_DRIVER', 'gd'),

GD and Imagick use Intervention Image v3 under the hood. You can override the driver per-image:

$request->image('photo')->using('imagick')->cover(200, 200);

The Cloudflare driver uses the Cloudflare Images API — uploading the image, applying transformations via URL parameters, and downloading the result:

// .env
CLOUDFLARE_IMAGES_ACCOUNT_ID=your-account-id
CLOUDFLARE_IMAGES_API_TOKEN=your-api-token

Custom drivers can be registered via extend():

use Illuminate\Support\Facades\Image;

Image::extend('custom', function ($app) {
    return new CustomDriver;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants