Introduction
Laravel provides a fluent image manipulation API through theImage facade. You can express operations such as resizing, cropping, encoding, and storing images in a consistent way across the framework.
Internally, it uses Intervention Image and supports both PHP’s GD and Imagick extensions.
This feature is available in Laravel v13.20.0 and later. If you are using an older version, update the framework to the latest release with
composer update.Installation
Before using image manipulation features, install the Intervention Image package with Composer.Configuration
The configuration file is placed atconfig/image.php. If it does not exist, publish it with an Artisan command.
IMAGE_DRIVER environment variable. Supported drivers are gd and imagick.
Loading images
TheImage facade provides methods for creating image instances from several sources. Image contents are loaded lazily, so the underlying bytes are not read until processing or output is requested.
Uploaded files
Use theimage method to retrieve an uploaded image from the request. It returns null when the file is not present.
fromUpload method when creating an image from an UploadedFile instance.
file method.
Storage files
Use thefromStorage method to create an image instance from a file stored on a filesystem disk. The first argument is the file path and the second argument is the disk name.
image method.
Other sources
You can also create image instances from bytes, local paths, URLs, and Base64 strings.Manipulating images
Image instances are immutable. Each method returns a new instance with a transformation added to the processing pipeline, making method chaining natural. Transformations are applied in the order they are added, and encoding happens only once at the end.Resizing
Other transformations
Conditional transformations
Image instances support theConditionable trait, so you can apply transformations conditionally with when and unless.
Encoding
By default, images are encoded in their original format. Use the following methods to convert formats.quality method to set output quality from 1 to 100.
optimize method is a shortcut for format conversion and quality configuration. The default is WebP at quality 70.
Storing images
Use thestore method to store an image on a filesystem disk. Laravel automatically generates a unique filename and returns the stored path.
storeAs when you want to specify the filename.
storePublicly and storePubliclyAs when storing with public visibility.
false.
Retrieving image information
Use themimeType, extension, dimensions, width, and height methods to retrieve image information. These methods reflect the processed image. For example, calling width() after cover(400, 400) returns 400.
Custom drivers
Laravel’s image manager extendsIlluminate\Support\Manager, so you can register custom drivers with the extend method.
A custom driver implements the Illuminate\Contracts\Image\Driver interface. Its process method receives the original image bytes and the pipeline, then returns the processed image bytes.
boot method.
using method to select a driver for a specific image.
Custom transformations
Define custom transformations with classes that implement theIlluminate\Contracts\Image\Transformation interface.
boot method.
transform method.