Share this:

Learn how to add watermarks in Laravel 10 and protect your images. This tutorial guides you through the process with Intervention Image package. Images are often a vital part of any website or application. They can help to make your content more engaging, eye-catching, and informative. However, if you don’t protect your images, they can easily be stolen or misused by others without your permission.

Adding watermarks to your images is an effective way to protect them from unauthorized use. In this tutorial, we will show you how to easily add watermarks to your images in Laravel 10 using the Intervention Image package.

Steps on How to Easily Add Watermarks in Laravel 10

  1. Install the Intervention Image package
  2. Create a route and controller method
  3. Create the watermark image
  4. Test the watermarking function
  5. Conclusion

Step 1: Install the Intervention Image package:

First, you need to install the Intervention Image package via composer by running the following command in your Laravel project directory:

composer require intervention/image

Step 2: Create a route and controller method:

Next, create a route and a controller method to handle the image watermarking. For example, you can create a route like this in your web.php file:

Route::get('/add-watermark', 'ImageController@addWatermark');

And then create a new controller method like this in your ImageController.php file:

use Intervention\Image\Facades\Image;

public function addWatermark()
{
    // Open the original image file
    $image = Image::make(public_path('images/original.jpg'));

    // Add the watermark image on top of the original image
    $watermark = Image::make(public_path('images/watermark.png'))
        ->opacity(50)
        ->resize($image->width() / 2, $image->height() / 2);

    $image->insert($watermark, 'center');

    // Save the watermarked image
    $image->save(public_path('images/watermarked.jpg'));

    // Return a response with the watermarked image
    return response()->file(public_path('images/watermarked.jpg'));
}

Step 3: Create the watermark image:

Next, create the watermark image that you want to add to your images. You can use an image editing software like Photoshop or GIMP to create a PNG image with a transparent background.

Step 4: Test the watermarking function:

Finally, test the watermarking function by visiting the URL that you created in step 2. If everything is set up correctly, you should see a watermarked version of the original image.

Conclusion:

Adding watermarks to your images is an easy and effective way to protect them from unauthorized use. By following the steps in this tutorial, you can easily add watermarks to your images in Laravel 10 using the Intervention Image package.

Share this:

Categorized in: