To reset or change an image size in Laravel, you can use the Intervention Image
library. This library allows you to manipulate images, including resizing them.
Steps to Reset Image Size in Laravel
1. Install Intervention Image Package
Run the following command in your terminal to install the package:
composer require intervention/image
2. Configure the Service Provider
If you’re using Laravel 5.5 or later, the service provider and facade will be auto-discovered. Otherwise, you may need to add the following to your config/app.php
file:
- Add the service provider:
Intervention\Image\ImageServiceProvider::class,
Add the alias:
'Image' => Intervention\Image\Facades\Image::class,
Add the alias:
'Image' => Intervention\Image\Facades\Image::class,
- Publish Configuration (Optional)
You can publish the configuration file with this command if you need to customize settings:
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravelRecent"
4. Resize the Image
Here’s an example of how to resize or reset an image size:
Controller Example:
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
class ImageController extends Controller
{
public function resizeImage(Request $request)
{
// Validate the uploaded image
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
// Get the uploaded image
$image = $request->file('image');
// Resize the image
$imagePath = public_path('images/resized'); // Destination folder
if (!file_exists($imagePath)) {
mkdir($imagePath, 0777, true); // Create folder if not exists
}
$imageName = time() . '.' . $image->getClientOriginalExtension();
// Use Intervention Image to resize
$resizedImage = Image::make($image->getRealPath());
$resizedImage->resize(300, 300, function ($constraint) {
$constraint->aspectRatio(); // Maintain aspect ratio
$constraint->upsize(); // Prevent upsizing
});
// Save the resized image
$resizedImage->save($imagePath . '/' . $imageName);
return back()->with('success', 'Image resized successfully!')->with('image', 'images/resized/' . $imageName);
}
}
5. Create a Form to Upload Images
In your Blade view file, create a form to upload images:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Resize Image</title>
</head>
<body>
@if(session('success'))
<p>{{ session('success') }}</p>
<img src="{{ asset(session('image')) }}" alt="Resized Image">
@endif
<form action="{{ route('resize.image') }}" method="POST" enctype="multipart/form-data">
@csrf
<label>Select an Image:</label>
<input type="file" name="image">
<button type="submit">Resize Image</button>
</form>
</body>
</html>
6. Add a Route
Define a route in web.php
:
use App\Http\Controllers\ImageController;
Route::get('resize-image', function () {
return view('resize-image'); // Blade file name
});
Route::post('resize-image', [ImageController::class, 'resizeImage'])->name('resize.image');
7. Output
When you upload an image, it will be resized to the specified dimensions (300x300
in the above example) and saved in the public/images/resized
directory.
You can adjust the size in the resize()
method as needed. For example:
$resizedImage->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
});
This will resize the width to 500 pixels while maintaining the aspect ratio.