Imager Pretransform

Pretransform any Assets on save, with Imager

Installation #

To install Imager Pretransform, follow these steps:

  1. Download & unzip the file and place the imagerpretransform directory into your craft/plugins directory
  2. Install plugin in the Craft Control Panel under Settings > Plugins
  3. The plugin folder should be named imagerpretransform for Craft to see it.

Imager Pretransform works on Craft 2.6.x.

Imager Pretransform Overview #

If you have suffered from memory or execution time issues when creating many transforms on demand, you should look into pre-generating transforms per image.

This plugin, combined with a long cache duration time, will make sure transforms will only be generated once, and one at a time.

When users upload an Asset, a task will be created, which in turn will use Imager to pre-generate the transform(s).

Configuring Imager Pretransform #

You can either have a set of transforms per Asset source handle:

<?php
return [
    'transforms' => [
        // Images source, with handle images
        'images' => [
            [
                'width' => 1400,
            ],
            [
                'width'       => 600,
                'jpegQuality' => 65
            ],
            [
                'width'       => 380,
                'height'      => 380,
                'mode'        => 'crop',
                'position'    => 'center-center',
                'jpegQuality' => 65
            ],

            'defaults' => [

            ],

            'configOverrides' => [
                'resizeFilter'         => 'catrom',
                'instanceReuseEnabled' => true,
            ]
        ],

        'anotherSourceHandle' => [
            [
                'height' => 600
            ]
        ]
    ]
];

Or just a set of transforms that will be applied to all Assets on upload/save:

<?php
return [
    'transforms' => [
        [
            'width' => 1400,
        ],
        [
            'width'       => 600,
            'jpegQuality' => 65
        ],
        [
            'width'       => 380,
            'height'      => 380,
            'mode'        => 'crop',
            'position'    => 'center-center',
            'jpegQuality' => 65
        ],

        'defaults' => [],
        'configOverrides' => []
    ]
];

You should also set Imager's cache duration to a long time, say 1 year:

In imager.php:

'cacheDuration' => 31536000, // 1 year

If any of your transform settings depends on a value from the specific Asset, you can pass a function instead of a string.

The function will be passed the AssetFileModel.

As an example, this is how you would use a Focal Point field:

<?php
return [
    'transforms' => [
        [
            'width'       => 400,
            'height'      => 400,
            'mode'        => 'croponly',
            'position'    => function ($asset) {
                return $asset->focalPointField;
            },
        ],
    ]
];

Using Imager Pretransform #

There is 3 ways to pretransform images:

  • Automatically on upload
  • Manually through element action
  • imagerpretransform console command that takes either folderId or sourceHandle as argument

Frequently asked questions

Can I target a specific Asset field?

This is not possible yet, but i'm considering it.

Releases

1.0.4 — 2017-09-13 Download
  • Improved: Will now skip SVGs
1.0.3 — 2017-09-05 Download
  • Improved: Non-image Assets will now be skipped.
1.0.2 — 2017-08-25 Download
  • Added: Added element action to pretransform images manually from Assets
  • Added: Added console command that takes either folderId or sourceHandle as argument
1.0.1 — 2017-07-09 Download
  • Improved: It's now possible to pass a function to any transform setting, so you do dynamic transform settings based on the Asset fields.
1.0.0 — 2017-07-07 Download
  • Added: Initial release

Currently available for Craft consulting and custom plugin development

Contact me