Filesize hook don't work

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);
}

This line doesn’t make sense. You can’t get the image size from $file->width() nor does that deliver an array to get the list of variables from.

list($width, $height) = getimagesize($file->root());

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?

Cheers

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,
thank you very much for the link.
I solved this now with two own functions with where I use GD-Lib.

If someone will need this also:

kirby()->hook('panel.file.upload', function($file) {
    $fileParts = pathinfo($file->root());
    if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'jpeg' || $fileParts['extension'] == 'png' || $fileParts['extension'] == 'bmp' || $fileParts['extension'] == 'tiff' || $fileParts['extension'] == 'tif'){
        try {
            list($width, $height) = getimagesize($file->root());
            if ($width > $height){
                // Landscape
                shrinkImage($file->root());
            } else {
                // Portrait or Square
                createLandscape($file->root());
            }
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }
});

function shrinkImage($filePath){
    $fileParts = pathinfo($filePath);
    $savePath = $filePath;  //full path to saved png, including filename and extension

    if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'jpeg'){
        $img = imagecreatefromjpeg($filePath);
    }

    if($fileParts['extension'] == 'png'){
        $img = imagecreatefrompng($filePath);
    }

    $imgWidth = imagesx($img);
    $imgHeight = imagesy($img);
    $width  = 800;
    $height = 534;
    $endPosX = ($width - $imgWidth) / 2;

    //calc the IMG aspectratio
    $ratioHeight = ($imgHeight * 1) / $imgWidth;
    $newHeight = ($imgWidth * $ratioHeight) / 1;

    //Resize the image
    $imgresize = imagescale($img, $width, $height);

    //save as jpg
    imagejpeg($imgresize, $savePath, 100);
}

function createLandscape($filePath){
    $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);
    }

    if($fileParts['extension'] == 'png'){
        $img = imagecreatefrompng($filePath);
    }

    $imgWidth = imagesx($img);
    $imgHeight = imagesy($img);
    $width  = 800;
    $height = 534;

    //calc the IMG aspectratio
    $aspectRatioHeight = ($height * 1) / $width;
    $newAspectRatioWidth = ($height * $aspectRatioHeight) / 1;

    //calc middle position
    $endPosX = ($width - $newAspectRatioWidth) / 2;

    //Resize the image
    $imgresize = imagescale($img, $newAspectRatioWidth, $height);

    //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, $imgresize, $endPosX, 0, 0, 0, $newAspectRatioWidth, $height);

    //save as jpg
    imagejpeg($backgroundImg, $savePath, 100);
}

Eventually I’ll make a plugin with config variables, if I find time for it.

Cheers and Thanks

Hi,
now in the production mode with upload multiple images, some of them have 20MB I have two problems.

  • Some Pictures get a bad qualitiy
  • Some pictures will not be resized

The code is in my post before.
here you can see the bad quality on the roofedge and the bell.

What could be the reason for that?

Are you using GD or ImageMagick driver?

If files are not created you’re probably running into a timeout.

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.

The files are pretty huge.

I meant themax_execution_time. Have you tried to switch to IM?