I am trying to understand how do I crop or resize an image on create or replace hook.
I’ve read @thguenther kirby-autoresize plugin code, but I don’t fully understand it.
The relevant part of the code is:
kirby()->thumb($file->root(), $file->root(), [
'width' => $maxWidth,
'height' => $maxHeight,
]);
Does that directly replace the image ? Why is $file->root() being passed twice as thumb’s arguments? I don’t see that use documented.
Also, could this be done via crop() , and how to make it replace the image aswell?
I imagined I would need to use replace() in order to replace the uploaded image with its cropped version.
Thank you
Yes, it will replenish the. original file, because it uses the same destination as the root. If you want to crop instead of resize, you can add the crop option to the options array.
1 Like
Ah! my bad, I was looking at $file->thumb()
It is clear now, thank you.
Thank you, I am having difficulties with properly providing the right options to crop within kirby()->thumb().
I want to provide a crop position, this is my attempt (in a plugin)
<?php
function squarecat($file) {
if ($file->template() == 'cat') {
$m = min($file->dimensions()->width(), $file->dimensions()->height());
try{
kirby()->thumb($file->root(), $file->root(), [
'crop' => 1, 1, ['crop' => 'center']
]);
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
}
Kirby::plugin('jaume/squarecat', [
'hooks' => [
'file.create:after' => function ($file) {
squarecat($file);
},
'file.replace:after' => function ($newFile, $oldFile) {
squarecat($newFile);
}
]
]);
This does not return any errors, but does not seem to affect the uploaded file
Thank you
kirby()->thumb($file->root(), $file->root(), [
'width' => $m,
'height' => $m,
'crop' => 'center',
]);
1 Like
Aaah, yes, that makes sense, thank you.