in Kirby 2 i had this hook working to automatically resize images on upload. However i cannot get this to work in Kirby 3. Is this still possible? Does anybody know how to do it?
Kirby 2 Code below:
kirby()->hook('panel.file.upload', 'resizeImage');
kirby()->hook('panel.file.replace', 'resizeImage');
function resizeImage($file) {
// set a max. dimension
$maxDimension = 850;
try {
// check file type and dimensions
if ($file->type() == 'image' and ($file->width() > $maxDimension)) {
// get the original file path
$originalPath = $file->dir() . '/' . $file->filename();
// create a thumb and get its path
$resizedImage = $file->resize($maxDimension,null,80);
$resizedPath = $resizedImage->dir() . '/' . $resizedImage->filename();
// replace the original image with the resized one
copy($resizedPath, $originalPath);
unlink($resizedPath);
}
} catch (Exception $e) {
return response::error($e->getMessage());
}
}
I find βqualityβ to work great with jpg but not so much with png.
I saved one png image at around 2.6mb and optimized it down to 1mb with tinypng. I then uploaded it to kirby with my hook quality at 70. The file came out at 2.9mb.
This might be super duper over-complicated but is there a way to compare the original file size to the new one and if the file size of the new one is greater than the original, keep the original.