Resizing uploaded images in Panel

Hi,
A question related to the thread here : sRGB conversion and resize upon upload… which unfortunately doesn’t works for me.

I’ like to know if the function upload or checkUpload inthe class FilesController can be extended to pass a SimpleImage fuction like :

   if($file = $upload->file()) {
      try {
        $this->checkUpload($file, $blueprint);
        kirby()->trigger('panel.file.upload', $file);
    
        if($file->type() == 'image' ) {

          $img = new Media($file);

          thumb($img, array('width' => 1200, 'height' => 1200));
       
        }

        return response::success('success');
      } catch(Exception $e) {
        $file->delete();
        return response::error($e->getMessage());
      }
    } else {
      return response::error($upload->error()->getMessage());
    }

I obviously tried this and no luck…
Can someone leads me to the right way to simply add a resize function that triggers when an image is uploaded to the panel other than set a max height/width in the blueprint ?

1 Like

Hi all,

I don’t get it…
Well with Kirby 2.1.0, it looks like the easiest way to trigger an action based on panel’s behaviour is now to work with a hook.

With the help of the imageconverter plugin (or even SimpleImage) , I’m still struggling to get a resize on upload (stating every file uploaded will be an image, i avoided to get the $file->type().

in site/config/config.php :

kirby()->hook('panel.file.upload', function($file) {
  
  
  $image = new ImageConverter($file, array(
      'height'=>600,
      'tosRGB' => false,
      'filename' => '{name}_resized.{extension}'
  ));

  $image->process();

  
});
```

Well this does …nothing.
I don't see any 500 status code returned in the developer tools / network tab on chrome, so I'm guessing the function has been a success .

What can be wrong with this code ? 

thanks for any hint / tip / help

Which ImageConverter plugin are you using?

This one developed by @seehat : https://github.com/madergrafisch/kirby-plugins/tree/master/imageconverter

Do you have ImageMagick installed and working properly and does your server allow php exec()?

If you want to resize the uploaded image you have to remove the filename option. This option creates a new file with the given dimensions.

Yes,
I’m developing on my own local server through Mamp and yes, ImageMagick is actually installed and used as the thumbnail driver in my config file.

— no success wether the filename or any other option set in the function.

Additionally :

  • the $kirby->plugins() line is set on my index.php
  • your plugin is correctly installed in the /site/plugins folder

Have you set a different path to the ImageMagick ‘convert’ script? - I think that the problem is up to ImageMagick. Now I have tested the code above and it works with 2.1.0.

$kirby->plugins() in index.php is not necessary anymore, when using version 2.1.0.

I think you’re right - I’ve tried with gd as my thumbs driver and simpleimage to process the uploaded file and although no image is processed --which is really unexpected, it looks like my local MAMP install is broken…
Still, on MAMP I’ve setup php 5.5.10 with the extension imagick.so uncommented in the php.ini but this don’t work any better.

Will try on remote host and doublecheck the install

Thanks for your help

—> Yes, shitty local install confirmed :skull: as it works like a charm on a well-configured server

I’m glad, that i was able to help you.