Hi,
I try to create a fileuploadhook for Kirby V2.
I need the possibility to make landscapeimages from portraitimages.
For this I have written a function and now I try to use this inside a hook.
The function works also alone but if I use it inside my hook It will not change my images on upload.
I would appreacheate if someone could take a look on my hook if I have a mistake in it?
kirby()->hook('panel.file.upload', function($file) {
try {
list($width, $height) = getimagesize($file->width());
if ($width > $height) {
// Landscape
$file->resize(1200, 810);
} else {
// Portrait or Square
createLandscape($file->url());
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
});
function createLandscape($filePath){
//$filePath = '003.jpg'; //full path to your png, including filename and extension
$fileParts = pathinfo($filePath);
$savePath = $filePath; //full path to saved png, including filename and extension
$colorRgb = ['red' => 229, 'green' => 231, 'blue' => 232]; //background color
if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'jpeg'){
$img = @imagecreatefromjpeg($filePath);
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
$width = 1200;
$height = 810;
$endPosX = ($width - $imgWidth) / 2;
}
//create new image and fill with background color
$backgroundImg = @imagecreatetruecolor($width, $height);
$color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
imagefill($backgroundImg, 0, 0, $color);
//copy original image to background
imagecopy($backgroundImg, $img, $endPosX, 0, 0, 0, $imgWidth, $imgHeight);
//save as jpg
imagejpeg($backgroundImg, $savePath, 100);
}
Hi Sonja,
yes width()makes here really no sense, here I need the file. But root() is also new for me.
Now the else works fine but the $file->resize(1200, 810); will not execute if I upload a landscape image.
How can I debug a hook? I tried to throw an excerption in the try-block but I don`t get it. Is there a possibility to send a message to the Kirby-Upload-Ajax back?
The hook doesn’t return anything. You can write to the error console or to file to do some debugging.
It surprises me thought, that $file->resize() shouldn’t work. You really don’t get anything in your thumbs folder if you upload a file? Is the folder writable? Do other thumbs get created?
Hi,
ah I thought it resizes my image on upload so that the file in the panel get the new size and not the thumb. Okay than it works.
But I need to change the uploaded file not only the thumb. I want this because I know that the user is uploading highend images and this will use a lot of serverspace.
Is this also with a Kirbymethod possible or do I need my own like on my createLandscape function?
Yes, but then you can’t use the resize() method like you do because that automatically creates a thumb. Someone posted an example for this, maybe there is even a plugin.
I thought so:
You can just copy the code you need from the plugin example.
Hi,
this should be all GD.
The files will be created but without my resizing. Which timeout could this be.
The memory_size I have scaled up to 60MB in my .htaccess.