In this Laravel image watermarking tutorial, we will see how to add text overlay watermark onto the image in the laravel 8 application.
Image watermarking is the process of adding text info over the image; it might display copyright info, any important info on the images.
This is also known as a digital sign, over and above, that used to verify the authenticity of the content.
Intervention Image is a profound open source lib especially used for image manipulation in PHP based projects. It offers pragmatic solution to edit an image in drastically less time, and it comes with two sublime processing lib Imagic even more GD library.
How to Add Watermark on Image in Laravel
- Step 1: Create Laravel Project
- Step 2: Install PHP Image Intervention Package
- Step 3: Update Image Intervention in Laravel
- Step 4: Create and Set Up Controller
- Step 5: Register New Routes
- Step 6: Create File Upload View File
- Step 7: Start Laravel Application
Create Laravel Project
It is obvious to start with installing a new laravel application obviously; this step is like a piece of cake; however, you may ignore this step if the app is already downloaded.
Install PHP Image Intervention Package
Ideally, this step reveals how to add a PHP intervention image library into the laravel app, well its no rocket science. You can invoke the installation process with the given below single command.
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.
Create and Set Up Controller
Next, run command to generate controller file.
Furthermore, get into the newly generated app/Http/Controllers/ImageFileController.php file and add the code.
Ensure that you have created an uploads folder into the public directory; after uploading the image, the files will get into the public/uploads directory.
The text() method is used to write text on the image; you can set the basepoint of the initial character using the x, y position. Moreover, it is easy to include more details such as font-file, font size, and text alignment. Here are the full options that you can try with the image intervention library.
Register New Routes
Subsequently, get into the routes/web.php; in this file, you need to add two routes. These routes handle displaying the view template at the same time, manipulating the image.
Create File Upload View File
The following code snippet creates a file upload form with the help of the Bootstrap 5 form control component. In the form tag, we define the route name, which makes the request to create a watermark on the image. Simultaneously, we are showing the processed image after image uploading.
Update resources/views/welcome.blade.php file.
Start Laravel Application
Ultimately, this is the last step; we have to kill the two birds with one stone in this step. First, start the laravel development server, then copy the manifested url on the terminal and run the app on the browser.