I would like to create an addon able to regenerate all thumbs of cockpit instance and register each url in data base associate to each asset.
I solved the second part with a responsive image addon who register thumbs url in each asset object :
addons/ResponsiveImage/bootstrap
function responsiveImage(string $assetPath): array
{
// available sizes
$widths = [640, 1024, 1440];
// ...
// prepare data
$data = [];
// map each sizes
foreach ($widths as $width) {
// build thumbnail
$url = cockpit('cockpit')->thumbnail([
'src' => $assetPath,
'width' => $width,
]);
// push result in an array
$data[] = [
'url' => $url,
'width' => $width
// ...
];
}
return $data;
}
$app->on('cockpit.assets.save', function (&$assets) use ($app) {
foreach ($assets as &$asset) {
$asset['thumbs'] = responsiveImage(COCKPIT_DIR . '/storage/uploads' . $asset['path']);
}
});
Now each asset got is own thumbs registered in database object when I upload a new file.
cockpit.assets.save
is exectuted when the file is upload but I didn’t find in documentation something about when asset is update for example.
Any suggestion ?