Laravel 8 Add Watermark on Image

    In this post we will learn how to add watermark on image in laravel 8. here we will see example of laravel 8 add watermark on image.

    When you want to display any text like any important information, any copyright content, your website or any other name in image at that time we can use watermake text. So, in this example i will show you how to add watermark text on image in laravel.

    Here we will use PHP image intervention library for Intervention image watermark text example or laravel 8 add text overlay watermark on image.

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

    Step 1: Create New Laravel Application For Laravel 8 Add Watermark on Image

    Step 2: Install Image Intervention Package For Add Watermark Text on Image in Laravel

    Step 3: Update Config/app.php File

    Step 4: Create Controller

    Step 5: Add Route

    Step 6: Create Blade File For Upload image

Step 1: Create New Laravel Application For Laravel 8 Add Watermark on Image

    First of all we are creting new project for laravel 8 add text overlay watermark on image example.

composer create-project --prefer-dist laravel/laravel Blog
Step 2: Install Image Intervention Package For Add Watermark Text on Image in Laravel

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

composer require intervention/image
Step 3: Update Config/app.php File

    In this step we will add providers and aliases in Config/app.php file.

<?php

return [

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

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

]
Read Also : Autocomplete Search using Bootstrap Typeahead JS
Step 4: Create Controller

    Here, we will create AddImageController.

php artisan make:controller AddImageController
<?php

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


class AddImageController extends Controller
{

    public function index()
    {
        return view('welcome');
    }
 
    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('© 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 upload folder in your public directory, path look like public/upload.

Step 5: Add Route
<?php

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


Route::get('/file-upload', [AddImageController::class, 'index']);

Route::post('/add-watermark', [AddImageController::class, 'imageFileUpload'])->name('image.watermark');
Step 6: Create Blade File For Upload image

    Now, i have added below code in resources/views/welcome.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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">

    <title>Laravel 8 Add Watermark on Image- websolutionstuff.com</title>
</head>

<body>
    <div class="container">
        <h1>Laravel 8 Add Watermark on Image- websolutionstuff.com</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>

    Now, it is ready to run.

Read Also : Helper Function Example in Laravel 8
Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan