Hey all
I’m currently developing a site based on react three fiber. Since three.js requires textures to follow the power of two rule and since I don’t want to force the client to adjust all images manually I thougth about writing a little file.create:after
hook (my first hook by the way, so I’m unfortunately quite inexperienced).
The goal is to:
- Save the original aspect ratio in a hidden field for further use
- Resize the image to a power of two of
512×1024px
(e.g. for portrait images)
The problem I have is, that everything I tried so far keeps the aspect ratio of the image. Which makes total sense, of course, because that’s the desired behavior in let’s say 99% of cases
Here’s what I currently have (derived from kirby autoresize plugin):
<?php
function powerOfTwo($file) {
if($file->isResizable()) {
if ($file->width() < $file->height()) {
try {
kirby()->thumb($file->root(), $file->root(), [
'width' => 512,
'height' => 1024
]);
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
}
}
Kirby::plugin('medienbaecker/autoresize
[details="Summary"]
This text will be hidden
[/details]
', [
'hooks' => [
'file.create:after' => function ($file) {
$file->update(array(
'aspect' => $file->height() / $file->width()
));
powerOfTwo($file);
},
'file.replace:after' => function ($newFile, $oldFile) {
powerOfTwo($newFile);
}
]
]);
I thought thumb()
could be a way to achieve resizing images to a power of two, since I couldn’t find anywhere that thumb()
always keeps the aspect ratio (as it’s explicitely stated in the docs for example for resize()
).
Any ideas how to achieve that? Happy about any hint!