Hey there,
i am using a hook which converts the image-file after upload with the following function:
<?php
function minifyFile($file) {
$maxWidth = option('minifyFile.maxWidth');
$maxHeight = option('minifyFile.maxHeight');
if($file->isResizable()) {
if($file->width() > $maxWidth || $file->height() > $maxHeight){
try {
kirby()->thumb($file->root(), $file->root(), [
'width' => $maxWidth,
'height' => $maxHeight,
'format' => 'webp'
]);
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}
} else {
try {
kirby()->thumb($file->root(), $file->root(), [
'format' => 'webp'
]);
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
}
}
But the output image is low quality and very muddy. I also tried to pass the “quality” variable for the thumb, but i this is only related to JPG.
Any ideas?
Does this refer to the image that is created from this code, or the resulting image on the frontend, and if the latter, is the output further manipulated?
Hey Sonja,
thanks for your reply. This is refered to the image created from the code.
Hard to tell, what is your original image size and what is your target resize? Which image driver (standard gd or IM)?
Neither of them. I tried to realize this with thumb() but i found out after some research, that that is not the way to do convertations. I decided to use SimpleImage like that:
hook in index.php in my plugin
'file.create:after' => function ($file) {
minifyFile($file, kirby());
},
function for convertation which i required in the index.php
<?php
function minifyFile($file, $kirby) {
$simpleImage = new \claviska\SimpleImage();
$page = $kirby->site()->page();
$maxWidth = option('minifyFile.maxWidth');
$maxHeight = option('minifyFile.maxHeight');
if($file->isResizable()) {
if($file->width() > $maxWidth || $file->height() > $maxHeight){
try {
$simpleImage->bestFit($maxWidth, $maxHeight);
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
try {
$simpleImage->fromFile($file->root())->toFile($page->root() . '/' . $file->name() .'.webp', 'image/webp');
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
return $page->image($file->name() . '.jpg');
}
The image does not get converted but i did not get any error messages, so i am not really sure how to debug PHP in such a situation.
Greetings and thank you very much
This here doesn’t make sense. You need to attach a file first.
You should get the page from the file object, in the context of your hook, you will otherwise just get the home page, I think.
So use $file->parent()
instead.
Ah damn, first one i overlooked and second one makes sence now
works also thank you