Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Image Laravel Laravel Package

intervention/image-laravel

Laravel integration for Intervention Image that provides a service provider, facade, and publishable configuration file, letting you set the image driver and options once centrally instead of repeating them on every image manager call.

View on GitHub
Deep Wiki
Context7

Intervention Image Laravel

Laravel Integration for Intervention Image

Latest Version Tests Monthly Downloads Support me on Ko-fi

This package provides an integration to setup Intervention Image easily to your Laravel application. Included are a Laravel service provider, facade and a publishable configuration file.

Requirements

  • Laravel >= 8

Installation

In your existing Laravel application you can install this package using Composer.

composer require intervention/image-laravel

Features

Although Intervention Image can be used with Laravel without this extension, this integration package includes the following features that make image interaction with the framework much easier.

Application-wide configuration

The extension comes with a global configuration file that is recognized by Laravel. It is therefore possible to store the settings for Intervention Image once centrally and not have to define them individually each time you call the image manager.

The configuration file can be copied to the application with the following command.

php artisan vendor:publish --provider="Intervention\Image\Laravel\ServiceProvider"

This command will publish the configuration file config/intervention-image.php. Here you can set the desired driver and its configuration options for Intervention Image. By default the library is configured to use GD library for image processing.

The configuration files looks like this.

return [

    /*
    |--------------------------------------------------------------------------
    | Image Driver
    |--------------------------------------------------------------------------
    |
    | Intervention Image supports “GD Library” and “Imagick” to process images
    | internally. Depending on your PHP setup, you can choose one of them.
    |
    | Included options:
    |   - \Intervention\Image\Drivers\Gd\Driver::class
    |   - \Intervention\Image\Drivers\Imagick\Driver::class
    |   - \Intervention\Image\Drivers\Vips\Driver::class
    */

    'driver' => env('IMAGE_DRIVER', \Intervention\Image\Drivers\Gd\Driver::class),

    /*
    |--------------------------------------------------------------------------
    | Configuration Options
    |--------------------------------------------------------------------------
    |
    | These options control the behavior of Intervention Image.
    |
    | - "autoOrientation" controls whether an imported image should be
    |    automatically rotated according to any existing Exif data.
    |
    | - "decodeAnimation" decides whether a possibly animated image is
    |    decoded as such or whether the animation is discarded.
    |
    | - "backgroundColor" Defines the default background & blending color.
    |
    | - "strip" controls if meta data like exif tags should be removed when
    |    encoding images.
    */

    'options' => [
        'autoOrientation' => true,
        'decodeAnimation' => true,
        'backgroundColor' => 'ffffff',
        'strip' => false,
    ]
];

You can read more about the different options for driver selection, setting options for auto orientation, decoding animations and background color.

Dependency Injection

The image manager can be used by resolving either Intervention\Image\ImageManager or Intervention\Image\Interfaces\ImageManagerInterface at various points in the application via dependency injection, resulting in a manager instance based on the application-wide image configuration.

The following example illustrates this using a controller.

use Intervention\Image\Interfaces\ImageManagerInterface;
use Illuminate\Http\Request;

class ImageController extends Controller
{
    /**
     * Create a new controller instance by injecting an image manager.
     */
    public function __construct(
        protected ImageManagerInterface $imageManager,
    ) {}

    /**
     * Handle image upload.
     */
    public function handleImageUpload(Request $request): RedirectResponse
    {
        $image = $this->imageManager->decodeSplFileInfo($request->file('image'));
    }
}

Static Facade Interface

This package also integrates access to Intervention Image's central entry point, the ImageManager::class, via a static facade. The call provides access to the centrally configured image manager via singleton pattern.

The following code example shows how to read an image from an upload request the image facade in a Laravel route and save it on disk with a random file name.

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Laravel\Facades\Image;

Route::get('/', function (Request $request) {
    $upload = $request->file('image');
    $image = Image::decode($upload);
    $image = $image->resize(300, 200);

    Storage::put(
        Str::random() . '.' . $upload->getClientOriginalExtension(),
        $image->encodeUsingFileExtension($upload->getClientOriginalExtension(), quality: 70)
    );
});

Image Response Macro

Furthermore, the package includes a response macro that can be used to elegantly encode an image resource and convert it to an HTTP response in a single step.

The following code example shows how to read an image from disk apply modifications and use the image response macro to encode it and send the image back to the user in one call. Only the first parameter is required.

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Format;
use Intervention\Image\Laravel\Facades\Image;

Route::get('/', function () {
    $image = Image::decode(Storage::get('example.jpg'));
    $image = $image->scale(300, 200);

    return response()->image($image, Format::WEBP, quality: 65);
});

You can read more about intervention image in general in the official documentation of Intervention Image.

Authors

This library is developed and maintained by Oliver Vogel

Thanks to the community of contributors who have helped to improve this project.

License

Intervention Image Laravel is licensed under the MIT License.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hotmeteor/eco-env
mvaliolahi/hydrate
wearede/tbcpay-php
parsnick/steak
thibaud-dauce/mattermost-php
multilinguals/apollo-client
antaresbr/collection
kr0lik/phpstan-rules
loco/loco
starfederation/datastar-php
yupmin/modern-phystrix
stephenjude/extended-artisan-commands
lumphp/laravel-macroable
lumphp/laravel-collections
lumphp/laravel-contracts
sermepa/sermepa
arkecosystem/fortify
arkecosystem/ui
owowagency/laravel-resources
gtjamesa/php-standards