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.
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.
string $originalPath
The image location, relative to Contao root.
int $width
The target image width.
int $height
The target image height.
string mode
The resize mode.
string $cacheName
The cache file name generated from source file name, width, height and mode setting.
\Contao\File $file
A \Contao\File
object from the source image.
string $targetPath
The target location where the image should be stored. This will be null
in
most cases, if not you should ignore $cacheName
.
\Contao\Image $imageObject
The instance of the \Contao\Image
class that triggered the hook.
If you want to override Contao’s Image::get
method, return a string to the new image.
Otherwise return the boolean false
.
// 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
{
// Return the path to a custom image
if (…) {
return $newImagePath;
}
return null;
}
}