How To Add Watermark On Image In Laravel 10

Websolutionstuff | Mar-31-2023 | Categories : Laravel

In this article, we will how to add a watermark to an image in laravel 10. Here we will learn about laravel 10 adding a watermark on the image. You can add image and also you can add text to the image. Here, we are using an intervention/image package in laravel 10.

When you want to display any text like any important information, any copyrighted content, your website, or any other name in the image at that time we can use watermark text. Here, we will use the PHP image intervention library for the Intervention image watermark text example

Intervention Image is a PHP image handling and manipulation library providing an easier and more expressive way to create, edit, and compose images. The package includes ServiceProviders and Facades for easy Laravel integration.

Intervention Image is an open-source library it is used for image manipulation in PHP-based projects.

So, let's see laravel 10 add a watermark to the image, laravel add a watermark on the Image, laravel 10 watermark pdf, laravel 10 add text to the image, and image intervention watermark in laravel 10.

Step 1: Install Laravel 10

In this step, we will install laravel 10 using the following command. So, run the below command to the terminal.

composer create-project --prefer-dist laravel/laravel laravel_10_watermark_example

 

Step 2: Install Image Intervention Package

Now, we will install the intervention/image package using the composer command in laravel 10.

composer require intervention/image

 

 

Step 3: Update Config/app.php File

In this step, we will add providers and aliases in the app.php file. This step is optional.

config/app.php

<?php

return [

	$providers => [
		
		'Intervention\Image\ImageServiceProvider'
	],

	$aliases => [
		
		'Image' => 'Intervention\Image\Facades\Image'
	]

]

By default, Intervention Image uses PHP's GD library extension to process all images. If you want to switch to Imagick, you can pull a configuration file into your application by running one of the following artisan commands.

Publish configuration in Laravel

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

 

Step 4: Create Controller

Here, we will create AddWatermarkImageController using the following command.

php artisan make:controller AddImageController

app/Http/Controllers/AddWatermarkImageController

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Image;


class AddWatermarkImageController extends Controller
{

    public function index()
    {
        return view('index');
    }
 
    public function imageFileUpload(Request $request)
    {
        $this->validate($request, [
            'file' => 'required|image|mimes:jpg,jpeg,png|max:4096',
        ]);

        $image = $request->file('file');
        $input['file'] = time().'.'.$image->getClientOriginalExtension();

        $imgFile = Image::make($image->getRealPath());

        $imgFile->text('© 2021 websolutionstuff.com', 100, 100, function($font) { 
            $font->size(50);  
            $font->color('#f1f1f1');  
            $font->align('center');  
            $font->valign('bottom');  
        })->save(public_path('/upload').'/'.$input['file']);          

        return back()
        	->with('success','File uploaded successfully ')
        	->with('fileName',$input['file']);         
    }
}

Note: Create an upload folder in your public directory, the path looks like public/upload.

 

Step 5: Add Route

Then, we will add routes to the web.php file.

routes/web.php 

<?php

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


Route::get('/', [AddWatermarkImageController::class, 'index']);

Route::post('/add-watermark', [AddWatermarkImageController::class, 'imageFileUpload'])->name('image.watermark');

 

 

Step 6: Create Blade File

Now, we are creating the index.blade.php file. So, add the following HTML to that file.

resources/views/index.blade.php

<!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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">

    <title>How To Add Watermark On Image In Laravel 10- Websolutionstuff</title>
</head>

<body>
    <div class="container">
        <h1>How To Add Watermark On Image In Laravel 10- Websolutionstuff</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 text-center">                
                <img src="/uploads/{{ Session::get('fileName') }}" width="100%"/>
            </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>

 


You might also like:

Recommended Post
Featured Post
How To Add Default Value Of Column In Laravel Migration
How To Add Default Value Of Co...

In this article, we will explore how to add default values to columns in Laravel 10 migrations, although the information...

Read More

May-01-2023

How To Show Loading Spinner In Ajax jQuery
How To Show Loading Spinner In...

In this article, we will see how to show loading spinner in ajax jquery. Using ajaxStart() and ajaxStop() method we...

Read More

Jul-06-2022

How to Create Login and Register in Node.js
How to Create Login and Regist...

As I embarked on my journey to develop a powerful web application, I realized the importance of a robust user authentica...

Read More

Oct-02-2023

How To Increase Session Lifetime In Laravel
How To Increase Session Lifeti...

In this article, we will see how to increase session timeout in laravel. In this example, we can see how to se...

Read More

Jun-28-2020