Can't implement file.upload hook

Dear all,

I am trying to implement a file.upload hook according to the instructions on kirby documentation and some other posts I found in the forum. However, I cannot get it working. What I want to do, is when uploading images in the panel and they are large ones, to have them automatically resized.

In config/config.php, this is what I have:

    kirby()->hook('panel.file.upload', function($file) {
    // your hook code
    $maxDimension = 1024;
    if ($file->width() > $maxDimension or $file->height() > $maxDimension) {
       // give a 1024x1024 image (for testing purposes) 
       $file->resize($maxDimension,$maxDimension);
     } 		
    });

Is there any additional configurations I need to do in another file or blueprint? What am I missing / doing wrong?
I am using kirby 2.4.0

Thanks in advance, Ilias

If you use the resize method, Kirby creates a thumbnail in the thumbs folder. So your hook works as it should, but not as you expect.

Have a look at the Kirby Image Shrink plugin for a possible solution.

The Image Shrink plugin doesn’t seem to resize when uploading.
In my thumbs I only have 75x75 versions.

The Shrink plugin replaces the file in the folder into which you upload the image, so if you use that plugin, the resized image will not be saved in the thumbs folder (apart from the 75x75 thumb that is created by the panel, of course). That’s what I thought you wanted?

Please check the file size of the uploaded image in the folder. If it is still the same original size, make sure that the plugin is correctly installed as /site/plugins/image-shrink/image-shrink.php. The folder name must be the same as the main plugin file!

This is how I have the plugin installed… However the image is saved in the original size, not reduced!

Could you please try to debug the plugin like this:

<?php
// Shrink large images on upload
kirby()->hook('panel.file.upload', 'shrinkImage');
kirby()->hook('panel.file.replace', 'shrinkImage');
function shrinkImage($file)
{
        // add write command here for debugging
       	f::write(kirby()->roots()->index() . DS . 'log.txt', 'hook works' . ' ' . $file->type(), true);


	$maxDimension = c::get('ka.image.shrink.maxDimension', 1000);
	
	try {
		if ($file->type() == 'image' and ($file->width() > $maxDimension or $file->height() > $maxDimension)) {
                       
                       // add write command here for debugging
			f::write(kirby()->roots()->index() . DS . 'log.txt', 'condition true', true);
			
                       // Get original file path
			$originalPath = $file->dir() . '/' . $file->filename();
			// Create a thumb and get its path
			$resized = $file->resize($maxDimension, $maxDimension);
			$resizedPath = $resized->dir() . '/' . $resized->filename();
			// Replace the original file with the resized one
			copy($resizedPath, $originalPath);
			unlink($resizedPath);
		}
	} catch (Exception $e) {
		return response::error($e->getMessage());
	}
}

Add the two f::write commands after the opening brackets of the function and right after the if-statement and let me know what you get.