php – Watermark in image on laravel 5.2

Question:

Hi, does anyone know how I could make every image that is uploaded to laravel has a watermark?

The image is sent through a form and through $request->file('img') in the controller I add in the folder and the name of the image in the database, but how to put this watermark in png on the image inside the method in controller ?

mergeimage() from php doesn't work.

Thanks

Answer:

Use the intervention/image package, it integrates easily with the Laravel framework following the installation step by step and then integration .

Step by step

$ php composer.phar require intervention/image

After installing the package on your Laravel, go to config/app.php and add this line to the array of providers :

Intervention\Image\ImageServiceProvider::class

and to make it easier type in the $aliases array

'Image' => Intervention\Image\Facades\Image::class

and to finish the installation and operation of the package, type in the command line

$ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

How to work with this package:

Route::get('/', function()
{
    $img = Image::make('foo.jpg')->resize(300, 200);

    return $img->response('jpg');
});

In your specific case, when saving the image to disk, load it again and call the insert method with the parameter of folder and filename with the extension .png to create the mask in your image and send it to save again.

Example:

// open an image file
$img = Image::make('public/foo.jpg');

// now you are able to resize the instance
$img->resize(320, 240);

// and insert a watermark for example
$img->insert('public/watermark.png');

// finally we save the image as a new file
$img->save('public/bar.jpg');

Source: Intervention Image

You can also use directly when uploading the photo:

Image::make($request->file('img')->getPathname());
Scroll to Top