tuscan88
tuscan88

Reputation: 5829

How to create and use image template with intervention image and Laravel 5.2

I am using the Intervention Image library in my Laravel 5.2 app along with the Image Cache plugin

I have been using the predefined templates out the box with no issues like so:

{{ route('imagecache', ['template' => 'medium', 'filename' => 'image.jpg']) }}"

I have seen in the documentation that as well as the default sizes small, medium and large you can create image filters to create custom manipulations and define them as a template in the config file so that instead of medium I would pass my template name. The documentation references Image filters as a way to do this but it's a bit sketchy as to exactly how to do it. Does anyone know how you do it exactly?

Upvotes: 0

Views: 1555

Answers (1)

Josh
Josh

Reputation: 1804

Inside config/imagecache.php there is a templates key, here you can add your own.

For example:

'templates' => [
    // ...
   'x-large' => 'App\Filters\ExtraLarge',
   // ...
],

Then you would just need to create the class App\Fitlers\ExtraLarge.

Inside the applyFilter() method you can call any methods on the $image property according to the documentation.

<?php

namespace App\Filters;

use Intervention\Image\Image;
use Intervention\Image\Filters\FilterInterface;

class ExtraLarge implements FilterInterface
{
    public function applyFilter(Image $image)
    {
        return $image->fit(1300, 1000);
    }
}

Then inside the route helper set the value of template to x-large

{{ route('imagecache', ['template' => 'x-large', 'filename' => 'image.jpg']) }}

Upvotes: 4

Related Questions