laravellaravel-backpackelfinder

Laravel elfinder resize and copy uploaded image to multiple dimensions


Hello I'm using backpack which uses elfinder as a file manager, and i want to achive te following:
when an image is uploaded I want to create 3 copies of it

  1. one with it real dimension
  2. the second is 200x200
  3. and the third 50x50

Note: Like WordPress which has this mechanism built in (uploading multiple sizes of the image)


Solution

  • Unfortunately, there's no easy way to do that thanks to elFinder itself. The PHP package for elFinder doesn't have events or anything like that, for you to customize its upload behaviour. And doing it in Javascript would probably not be a good idea.

    But thanks to Backpack, there's one thing you could do, that could be useful. You can use Model Events inside your CrudControllers, to do stuff after the cretate/update form has been submitted. For example:

    So it would looks something like this:

    public function setup()
    {
    
        // ...
    
        Product::saved(function($entry) {
            if (!$entry->hasThumbnailsForAttachment()) {
                $entry->generateThumbnailsForAttachment()
            }
        });
    }
    

    Then in your Model you could create these two methods, hasThumbnailsForAttachment() and generateThumbnailsForAttachment().

    Hope it helps!