Skip to content

Inserting watermark with event listeners.

Stream edited this page Oct 19, 2018 · 4 revisions

Thanks gwleuverink, pasted from #480.


You can add a watermark using a combination of the ImageWasUploaded Event and the Image facade provided by intervention image.

If you are not familiar with Laravel events I recommend you check out the docs or this video first.

First you need to create an event listener within your app that listens to the ImageWasUploaded event. Check out this page in the documentation for more info. Or refer to the demo project that provides usage examples here.

Next you need to add the watermark. You need to do this from within the event listener you just created. This filemanager uses the Intervention Image package. On this page you'll find an example on how to add a watermark.

Applying everything your event listener code should look something like the example below. I didn't test this but it will help you on your way.

class HasUploadedImageListener
{
    /**
     * Handle the event.
     *
     * @param  ImageWasUploaded  $event
     * @return void
     */
    public function handle(ImageWasUploaded $event)
    {
        // Grab the uploaded file's path
        $imagePath = $event->path();

        // Open the image file
        $img = Image::make($imagePath);

        // Insert the watermark
        $img->insert('public/watermark.png');

        // Save the image
        $img->save($imagePath);
    }
}