In this article, we'll see how to use intervention images in laravel 11. Here, we'll use the intervention/image-laravel composer package in laravel 11. Intervention Image is an open-source PHP image manipulation library.
Using this package you can crop(), blur(), canvas(), filter(), fill(), height(), insert(), width(), make(), reset(), resize(), save(), text(), rotate().
Also, we need two extensions enabled in your system.
In this step, we'll install 11 applications using the following composer command.
composer create-project laravel/laravel laravel-11-example
Then, we'll install the intervention/image-laravel package using the following command.
composer require intervention/image-laravel
Next, we'll define the routes into the web.php file
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;
Route::get('image-upload', [ImageController::class, 'index']);
Route::post('image-upload', [ImageController::class, 'store'])->name('image.store');
Now, we'll create a controller using the following command.
php artisan make:controller ImageController
app/Http/Controllers/ImageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Intervention\Image\Laravel\Facades\Image;
class ImageController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(): View
{
return view('image-upload');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request): RedirectResponse
{
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
]);
$image = Image::read($request->file('image'));
$imageName = time().'-'.$request->file('image')->getClientOriginalName();
$destinationPath = public_path('images/');
$image->save($destinationPath.$imageName);
$destinationPathThumbnail = public_path('images/thumbnails/');
$image->resize(100,100);
$image->save($destinationPathThumbnail.$imageName);
/**
* Write Code for Image Upload Here,
*
* $upload = new Images();
* $upload->file = $imageName;
* $upload->save();
*/
return back()->with('success','Image Upload successful')->with('imageName',$imageName);
}
}
Create images and thumbnails (inside the images folder) folder in the public folder.
Then, we'll create an image-upload.blade.php file.
resources/views/image-upload.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 11 Image Intervention Example - Techsolutionstuff</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3"><i class="fa fa-star"></i> Laravel 11 Image Intervention Example - Techsolutionstuff</h3>
<div class="card-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@session('success')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
<div class="row">
<div class="col-md-4">
<strong>Original Image:</strong>
<br/>
<img src="/images/{{ Session::get('imageName') }}" width="300px" />
</div>
<div class="col-md-4">
<strong>Thumbnail Image:</strong>
<br/>
<img src="/images/thumbnail/{{ Session::get('imageName') }}" />
</div>
</div>
@endsession
<form action="{{ route('image.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label class="form-label" for="inputImage">Image:</label>
<input
type="file"
name="image"
id="inputImage"
class="form-control @error('image') is-invalid @enderror">
@error('image')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Upload</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Now, run the laravel 11 application using the following command.
php artisan serve
You might also like:
Hello, laravel developers! In this article, we'll see how to update user profiles in laravel 11. Here, we'll cha...
Jul-12-2024
In this article, we will see laravel 10 select2 autocomplete search using ajax. Here, we will learn about sele...
Apr-10-2023
Welcome to "Mastering Reactive Forms Validation in Angular 15: A Comprehensive Guide." In this guide, we will...
Jun-14-2023
In this article, we will see that laravel 9 has many through relationship example. hasManyThrough relationship is diffic...
Apr-04-2022