Laravel 11 Resize Image Before Upload | websolutioncode.com
Laravel 11 Resize Image Before Upload | websolutioncode.com

Laravel 11 Resize Image Before Upload

Introduction:

In web development, handling images is a common task, and ensuring they are of appropriate size before uploading can be crucial for performance and user experience. Laravel, a widely-used PHP framework, offers handy features for manipulating images. In this tutorial, we’ll walk through how to resize image before upload using Laravel 11.

Step 1:

Setting Up Laravel 11 Firstly, make sure you have Laravel 11 installed on your system. If not, you can install it via Composer by running the following command in your terminal:

composer create-project --prefer-dist laravel/laravel resize-image-example

Step 2:

Creating Migration and Model Next, let’s create a migration and model to store our image data. Run the following commands in your terminal:

php artisan make:model Image -m

This will create a migration file and a model named Image.

Now, open the migration file created in the database/migrations directory and add the following code to define the schema for the images table:

public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->string('image_path');
$table->timestamps();
});
}

Then, run the migration to create the images table in your database:

php artisan migrate

Step 3:

Setting Up Routes and Views Let’s create routes and views to handle image uploading. Open the routes/web.php file and add the following route

Route::get('/upload', [ImageController::class, 'showUploadForm']);
Route::post('/upload', [ImageController::class, 'upload']);

Now, create a controller named ImageController by running the following command:

php artisan make:controller ImageController

Open the newly created ImageController.php file and add the following methods:

public function showUploadForm()
{
return view('upload');
}

public function upload(Request $request)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);

$imageName = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $imageName);

Image::create(['image_path' => $imageName]);

return back()
->with('success','Image uploaded successfully.')
->with('image',$imageName);
}

Step 4:

Creating the Upload Form Create a new file named upload.blade.php in the resources/views directory and add the following code

<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>

<div class="container mt-4">
<form action="/upload" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" name="image" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Upload Image</button>
</form>
</div>

</body>
</html>

Step 5:

Resizing Images Before Upload To resize images before uploading, we’ll use the intervention/image package. Install it via Composer by running the following command:

composer require intervention/image

Next, add the following code to the upload method in ImageController.php to resize the image before saving it:

use Intervention\Image\Facades\Image;

public function upload(Request $request)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);

$imageName = time().'.'.$request->image->extension();

$image = Image::make($request->image);
$image->resize(300, 200);
$image->save(public_path('images/'.$imageName));

Image::create(['image_path' => $imageName]);

return back()
->with('success','Image uploaded successfully.')
->with('image',$imageName);
}

That’s it! Now, when you upload an image using the form, it will be resized to 300×200 pixels before being saved to the public/images directory.

Conclusion:

In this tutorial, we’ve learned how to resize images before uploading in Laravel 11 using the intervention/image package. Resizing images before upload can help improve performance and optimize storage space. This technique can be particularly useful in applications where users frequently upload images. Feel free to experiment further with image manipulation in Laravel to suit your specific requirements.