In this tutorial you will learn about the Laravel 9 Add Text Overlay Watermark on Image Example and its application with practical example.
In this Laravel 9 Add Text Overlay Watermark on Image Example tutorial I will show you how to add text overly watermark on Image in laravel 9. In this tutorial you will learn to add or create text overly watermark on Image in laravel 9 application. In this article I will share example to create watermark on image in laravel 9. We will be using PHP image intervention library to create text overly watermark on Image. The PHP image intervention library is helpful in creating images with copyright information, digital signature and any important information on it. In laravel we will using intervention package for adding text overly watermark on Image.
Laravel 9 Add Text Overlay Watermark on Image Example
In this step by step tutorial I will demonstrate you with example how to add text overly watermark on Image in laravel 9. Please follow instruction given below:
-
- Install Laravel 9
- Connecting App to Database
- Install PHP Image Intervention Package
- Add Routes
- Create Controller
- Create Blade View
- Make Folder
- Start Development Server
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel LaravelIntervention |
Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Install PHP Image Intervention Package
In this step, we will install Image intervention Package via the composer dependency manager. Use the following command to install image intervention Package.
1 |
composer require intervention/image |
After Installing Image intervention package, we need to add service provider and alias in config/app.php file as following.
config/app.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php return [ $providers => [ ......, ......, 'Intervention\Image\ImageServiceProvider' ], $aliases => [ ......, ......, 'Image' => 'Intervention\Image\Facades\Image' ] ] |
Now, run following command to migrate database schema.
1 |
php artisan migrate |
Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ImageFileController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- */ Route::get('/file-upload', [ImageFileController::class, 'index']); Route::post('/add-watermark', [ImageFileController::class, 'imageFileUpload'])->name('image.watermark'); |
Create Controller
Now, lets create a controller named ImageFileController using command given below –
1 |
php artisan make:controller ImageFileController |
Then visit to app/controllers/ImageFileController.php and add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Image; class ImageFileController extends Controller { public function index() { return view('my-images'); } public function imageFileUpload(Request $request) { $this->validate($request, [ 'file' => 'required|image|mimes:jpg,jpeg,png,gif,svg|max:4096', ]); $image = $request->file('file'); $input['file'] = time().'.'.$image->getClientOriginalExtension(); $imgFile = Image::make($image->getRealPath()); $imgFile->text('© 2017-2021 w3adda.com - All Rights Reserved', 120, 100, function($font) { $font->size(35); $font->color('#ffffff'); $font->align('center'); $font->valign('bottom'); $font->angle(90); })->save(public_path('/uploads').'/'.$input['file']); return back() ->with('success','File successfully uploaded.') ->with('fileName',$input['file']); } } |
Create Blade View
In this step we will create blade view file. Go to /resources/views and create one file my-images.blade.php and add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!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>Laravel Image Manipulation Tutorial</title> </head> <body> <div class="container mt-4" style="max-width: 600px"> <h2 class="mb-5">Laravel Image Text Watermarking 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> |
Make Folder
In this step, Visit the public directory and create one folder or directory name upload.
1 2 |
Go to public folder => first create folder name upload |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/file-uploads |