How to add a watermark image on an image in Laravel?

Laravel provides several packages for image manipulation of your app with an open-source PHP image handling and manipulation library. here I will give you step-by-step instructions on how to add a watermark image on an image in the Laravel application. you can easily use Intervention Image with Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10 versions.

add watermark image to uploaded image in laravel

add watermark image before uploading image in Laravel

Intervention Image is a profound open-source library especially used for image manipulation in PHP-based projects.

It offers pragmatic solutions to edit an image in drastically less time, and it comes with two sublime processing libraries, Imagick even more GD library.

Okay, let’s get straight to the steps for adding the image on a image as watermark in the Laravel application with a preview👇

Add a watermark image on an image in Laravel

  • Step 1: Create a Laravel Project
  • Step 2: Install the PHP Image Intervention Package
  • Step 3: Update Image Intervention in Laravel
  • Step 4: Create and Set Up the Controller
  • Step 5: Register New Routes
  • Step 6: Create File Upload View File
  • Step 7: Start the Laravel Application

Create Laravel Project

It is obvious to start with installing a new Laravel application; this step is like a piece of cake; However, you may ignore this step if the app is already downloaded.

composer create-project --prefer-dist laravel/laravel watermark-app

Install PHP Image Intervention Package

Ideally, this step reveals how to add a PHP intervention image library into the Laravel app, Well it’s no rocket science. You can invoke the installation process with the given below single command.

composer require intervention/image

Update Image Intervention in Laravel

Now, the image intervention package is installed, then it is spontaneous that we register the essential classes in the providers and aliases array within the config/app.php file.

<?php
return [
	$providers => [
		......,
		......,
		'Intervention\Image\ImageServiceProvider'
	],
	$aliases => [
		......,
		......,
		'Image' => 'Intervention\Image\Facades\Image'
	]
]

Create and Set Up a Controller

php artisan make:controller WatermarkImageFileController

app/Http/Controllers/WatermarkImageFileController.php file and add the code.

<?php

namespace App\Http\Controllers;

use Image;
use Illuminate\Http\Request;

class WatermarkImageFileController extends Controller
{
    public function index()
    {
        return view('imagewatermark');
    }
 
    public function imageFileUpload(Request $request)
    {
        $this->validate($request, [
            'file' => 'required|image|mimes:jpg,jpeg,png,gif,svg|max:4096',
        ]);
        $watermark = public_path('/uploads/watermark.png');
        $image = $request->file('file');
        $input['file'] = time().'.'.$image->getClientOriginalExtension();
        $imgFile = Image::make($image->getRealPath())->resize(300, 300);
        $imgFile->insert($watermark, 'center');
        $imgFile->save(public_path('/uploads').'/'.$input['file']);          
        return back()
        	->with('success','File successfully uploaded.')
        	->with('fileName',$input['file']);         
    }
}

Ensure that you have created an uploads folder in the public directory; after uploading the image, the files will get into the public/uploads directory.

Register New Routes

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WatermarkImageFileController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/


Route::get('/file-upload', [WatermarkImageFileController::class, 'index']);
Route::post('/add-watermark', [WatermarkImageFileController::class, 'imageFileUpload'])->name('image.watermark');

Before inserting a file please, save a logo image to insert on the image for overlay in public/uploads/watermark.png.

insert image as watermark in another image in Laravel

Create File Upload View File

Update resources/views/imagewatermark.blade.php file.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>Add watermark image on a image in Laravel</title>
</head>
<body>
    <div class="container mt-4" style="max-width: 600px">
        <h2 class="mb-5">Watermark image on a image in Laravel Example</h2>
        <form action="{{route('image.watermark')}}" enctype="multipart/form-data" method="post">
            @csrf
            @if ($message = Session::get('success'))
            <div class="alert alert-success">
                <strong>{{ $message }}</strong>
            </div>

            <div class="col-md-12 mb-3 text-center">
                <strong>Manipulated image:</strong><br />
                <img src="/uploads/{{ Session::get('fileName') }}" width="600px"/>
            </div>
            @endif
            @if (count($errors) > 0)
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif
            <div class="mb-3">
                <input type="file" name="file" class="form-control"  id="formFile">
            </div>
            <div class="d-grid mt-4">
                <button type="submit" name="submit" class="btn btn-primary">
                    Upload File
                </button>
            </div>
        </form>
    </div>
</body>
</html>

Start Laravel Application

php artisan serve
http://127.0.0.1:8000/file-uploads 

I hope that this article helped you add a watermark image to the uploaded image example in the Laravel application. You may also want to check out our guide on converting time from 24-hour format to 12-hour AM/PM in Laravel.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Scroll to Top