getImage
Details
This page shows version 4.x. The current version can be found here.
The getImage hook is triggered when a thumbnail is generated and allows you to
add a custom routine. It passes the path, the width and height, the mode, the
cache name and the file object as arguments and expects a path as return value.
Note
Using the executeResize and getImage hooks has been deprecated and will no
longer work in Contao 5.0. Use the contao.image.resizer service instead.
Parameters
string
$originalPathThe image location, relative to Contao root.
int
$widthThe target image width.
int
$heightThe target image height.
string
modeThe resize mode.
string
$cacheNameThe cache file name generated from source file name, width, height and mode setting.
\Contao\File
$fileA
\Contao\Fileobject from the source image.string
$targetPathThe target location where the image should be stored. This will be
nullin most cases, if not you should ignore$cacheName.\Contao\Image
$imageObjectThe instance of the
\Contao\Imageclass that triggered the hook.
Return Values
If you want to override Contao’s Image::get method, return a string to the new image.
Otherwise return the boolean false.
Example
// src/EventListener/GetImageListener.php
namespace App\EventListener;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\Image;
use Contao\File;
#[AsHook('getImage')]
class GetImageListener
{
public function __invoke(
string $originalPath,
int $width,
int $height,
string $mode,
string $cacheName,
File $file,
string $targetPath,
Image $imageObject
): string|null
{
// Return the path to a custom image
if (…) {
return $newImagePath;
}
return null;
}
}