Easy Thumbnail Generation in PHP (Laravel Example)

Nowadays, thumbnail generation in PHP (or any other language) is a requirement for every modern Web App or Website we built. The time of resizing images on the front end is mostly gone. And, although thumbnail generation in PHP a relatively simple task already, the Intervention library makes it even more ridiculously easy.

Thumbnail Generation in PHP and Laravel

Installation

Require the library through composer:

composer require intervention/image

For PHP, just use the autoloaded class:

// 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'));

For Laravel, configure the provider and aliases in the config/app.php file. After that publish the configurations file add the necessary options at config/image.php:

// In the $providers array add the service providers for this package.
Intervention\Image\ImageServiceProvider::class

// Add the facade of this package to the $aliases array.
'Image' => Intervention\Image\Facades\Image::class

// Publish configuration in Laravel 5
$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

// Configure the image driver and other options on config/image.php file

 

Thumbnail Generation in PHP

Moving now to the good stuff, generating a thumbnail with Intervention is as easy as:

Image::make($imagePath)
        ->resize($width, $height, function ($constraint) {
            $constraint->upsize();
            $constraint->aspectRatio();
        });

With this previous command, you just resized the image to the given width and height while keeping the aspect ratio and prevent the image to being up sized. Easy right?

If you just want to make your image fit a given space, you can just call the fit function and pass the sizes to it like this:

Image::make($externalImagePath)
        ->fit($width, $height, function ($constraint) {
            $constraint->upsize();
        });

These are just two of the many transformation methods available like widen, flip, crop and so on. Check the documentation for more detailed information.

Practical Example – Post Model Thumbnail Generator

Let’s finish this quick article with a practical Laravel example. Imagine that you have a Post model and you want a method to generate the post image thumbnail on demand, this would be as easy as this:

public function generateImageThumbnail()
{
    // Create thumbnail from the post image and save it on the post folder
    $path = "/img/posts/{$this->id}/thumbnail.jpg";
    Image::make($this->image)
        ->resize(env('THUMB_WIDTH'), null, function ($constraint) {
            $constraint->upsize();
            $constraint->aspectRatio();
        })
        ->save(public_path() . $imgPath, 80);
    
    // Return the thumbnail url
    return env('APP_URL') . $path;
}