Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tommy001's avatar

Resize and optimize images upon upload with Laravel Filemanager

I am using Laravel Filemanager on my 5.7 site. I find this package really useful and it ticks all boxes except one. It would be great if images could be resized automatically on upload, if they are bigger than for ex. 1000 px width. Any ideas of how I can accomplish that? Anyone who has already done it?

0 likes
9 replies
Tudor90's avatar

@Sti3bas I I did exactly as you wrote, but when I upload an image in Filemanager, this event not fired and not exist any error. I use a laravel ecommerce package (Bagisto)

Tommy001's avatar

Thanks Sti3bas! I will give it a read. Sounds like the way to go.

Tommy001's avatar

Tray2: Since I want to automatically resize images uploaded with Laravel Filemanager, I would still have to listen for upload events from Laravel Filemanager? That's what you meant? Then I can use Intervention in my custom UploadListener class to resize the image. Not sure though exactly where to put my custom code to do this.

Sti3bas's avatar
Sti3bas
Best Answer
Level 53

Not sure though exactly where to put my custom code to do this.

Just create an event listener. php artisan make:listener ResizeUploadedImage

Then register it in EventServiceProvider:

protected $listen = [
   //...
   \UniSharp\LaravelFilemanager\Events\ImageWasUploaded::class => [
       \App\Listeners\ResizeUploadedImage::class,
   ],
];

ResizeUploadedImage listener:

use Intervention\Image\Facades\Image;

//...

public function handle($event)
{
    $image = Image::make($event->path());

    if($image->width() <= 1000) {
        return;
    }

    $image->fit(500, 500)
        ->save();
}

http://image.intervention.io/getting_started/installation#laravel

Tommy001's avatar

Sti3bas; My first tryout ended with an error message after image upload (Object object).

screen dump

If I can't figure it out I will post a question in the Support section. Thanks a lot again.

Tommy001's avatar

In case anyone else is as confused about this as I was, I will share the solution that finally worked. Sti3bas above put me on the right track. This is the code that worked for me:

The code in app/Providers/EventServiceProvider.php:

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use UniSharp\LaravelFilemanager\Events\ImageWasUploaded;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\Event' => [
            'App\Listeners\EventListener',
        ],
        ImageWasUploaded::class => [
         '\App\Listeners\ResizeUploadedImage',
     ],       
 ];

And in my event listener app\Listeners\ResizeUploadedImage.php (I decided to use 1100 px maximal width):

namespace App\Listeners;

use UniSharp\LaravelFilemanager\Events\ImageWasUploaded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Image;

class ResizeUploadedImage
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(ImageWasUploaded $event)
    {
        $path = $event->path();
        $image = Image::make($path);
        if($image->width() < 1100) {
            return;
        }
        // resize the image to a width of 1100 and constrain aspect ratio (auto height)
        $image->resize(1100, null, function ($constraint) {
            $constraint->aspectRatio();
        })->save($path);
    }
}
1 like
hungten's avatar

I am using ck finder and ck editor for my site. I find this package really useful and it ticks all boxes except one. It would be great if images could be resized, convert or optimize automatically on upload. Any ideas of how I can accomplish that? Anyone who has already done it?.

Please or to participate in this conversation.