Blog Detail

5

Aug
Resize & Upload Image using Intervention Image in Laravel cover image

Resize & Upload Image using Intervention Image in Laravel

Intervention Image is an open-source PHP image handling and manipulation library, That provides a more straightforward and meaningful way to create, edit, and compose images. It is written to make PHP image manipulation effortless and more expressive.

However, if you want to produce image thumbnails, watermarks, or format huge image files Intervention Image assists you to execute every task as easily with a few lines of code as possible. The package includes service providers and Facades for easy Laravel integration.

Requirements

  • Intervention Image requires the following components:
  • PHP >= 5.4
  • Fileinfo Extension
  • And one of the following image libraries.
  • GD Library (>=2.0) … OR …
  • Imagick PHP extension (>=6.5.7)

Installation

You've to run the following command for the installation purpose.

$ php composer.phar require intervention/image

Now your composer.json has been updated automatically and you're able to require the just created vendor/autoload.php file to PSR-4 autoload the library.

Usage

Intervention Image doesn't need Laravel or any other framework at all. If you want to utilize it, you just need the composer autoload file to illustrate image objects as shown in the following example.

For Example:

// include composer autoload
require 'vendor/autoload.php';

// import the Intervention Image Manager Class
use Intervention\Image\ImageManager;

// create an image manager instance with favored driver
$manager = new ImageManager(array('driver' => 'imagick'));

// to finally create image instances
$image = $manager->make('public/foo.jpg')->resize(300, 200);

You can also use the static version of ImageManager as shown in the example below.

Static Example

// include composer autoload
require 'vendor/autoload.php';

// import the Intervention Image Manager Class
use Intervention\Image\ImageManagerStatic as Image;

// configure with favored image driver (gd by default)
Image::configure(array('driver' => 'imagick'));

// and you are ready to go ...
$image = Image::make('public/foo.jpg')->resize(300, 200);

Integration in Laravel

If you want to integrate Intervention Image in Laravel then you can practice it easily. Because Intervention Image has optional support for Laravel and comes with a Service Provider and Facades for easy integration. Laravel already has a vendor/autoload.php, so you don't need to have it manually. Just see the instructions below.

After installing the intervention image, you've to open your Laravel config file config/app.php, and then you've to add the service providers in the $providers array for this package.

Intervention\Image\ImageServiceProvider::class

Next, Add the facade of this package to the $aliases array.

'Image' => Intervention\Image\Facades\Image::class

Configuration

Intervention Image uses PHP's GD library extension by default. You can easily switch to Imagick by running one of the following artisan commands.

Publish configuration in Laravel

$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravelRecent"

Publish configuration in Laravel <= 4

$ php artisan config:publish intervention/image

After that, the configuration file is copied to config/image.php. With this copy, you can modify the image driver settings for your application locally.

Example

// usage inside a laravel route
Route::get('/', function()
{
    $img = Image::make('foo.jpg')->resize(300, 200);

    return $img->response('jpg');
});

This package supports a lot of image formats. You can view its whole documentation and many other features on Github.

Published at : 05-08-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

web application development client
web application development client
web application development client
web application development client
web application development client

Launch project