What’s your actual goal? Do you want to resize uploaded images or do you want to reject uploads with wrong dimensions?
There are two events, that might be interesting:
// before moving file from temp folder to assets folder
$app->on('cockpit.asset.upload', function(&$asset, &$_meta, &$opts) {
if ($asset['width'] > 3000) {
// resize or reject image...
$asset = null; // should reject (I didn't test it)
}
});
// after moving file from temp folder to assets folder
$app->on('cockpit.asset.save', function(&$asset) {
// change something after
});
Here is some inspiration, how to modify assets (paths):
The actual goal is automaticaly resize uploaded images (not reject)
Finaly, assets folder will contain only proportionaly resized images to max. dimensions.
I’m not sure which event is more efficient for this purpose,
so for example:
// before moving file from temp folder to assets folder
$app->on('cockpit.asset.upload', function(&$asset, &$_meta, &$opts) {
if ($asset['width'] > 1920) {
// proportionaly resize image ...
// set only new values $asset['width'], $asset['height'] is not solution
// How can I do this? Any idea?
}
});
You can use this snippet to resize assets automatically. It’s no perfect solution, because it takes a while to process the files and there is no trigger to catch uploaded files before any rendering happens. It also won’t fix the too much RAM issue, if your resolution is too high.
The assets manager needs some refactoring in the future, but for now it works.
Thanks a lot Raffael!
I made some changes in your code snippet to allow this process with respect to the group configuration only + max. height option. It works good enough.