How to compress image before uploading in Laravel 11

How to compress image before uploading in Laravel



What is Intervention Image?

Intervention Image is a PHP library that provides an easy way to work with images in Laravel and other PHP applications. It offers a clean and simple API for tasks like resizing, cropping, and applying filters to images.

Some of the key features of Intervention Image include:
  • Resizing: Adjust the dimensions of an image.
  • Cropping: Cut out a portion of an image.
  • Manipulating: Apply various filters and effects to images.
  • Drawing: Add text or shapes to an image.

To use Intervention Image in Laravel, you would typically install it via Composer and configure it in your Laravel project. Here’s a basic example of how to use it:

Install the package:

composer require intervention/image-laravel

Next, add the configuration files to your application using the vendor:publish command:

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

OR

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

Output 


 INFO  Publishing assets.

  Copying file [E:\WEB PRO\TES IMAGE COMPRES\laravel\vendor\intervention\image-laravel\config\image.php] to [E:\WEB PRO\TES IMAGE COMPRES\laravel\config\image.php]  DONE


A new file will appear in the config folder

Config/image.php
<?php

return [

    'driver' => \Intervention\Image\Drivers\Gd\Driver::class,

    'options' => [
        'autoOrientation' => true,
        'decodeAnimation' => true,
        'blendingColor' => 'ffffff',
    ]
];

How to use

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Intervention\Image\Laravel\Facades\Image;


class ImageController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'image' => 'required|image|mimes:jepg,jpg,png,',
            // 'imagea' => 'required|image|mimes:jpeg,jpg,png',

        ]);

        $imagefile = $request->file('image');
        $fielname =   time() . "yosa" . $imagefile->getClientOriginalName();
        $image = Image::read($imagefile);

        // Resize the image to 50% of the original size
        $image->resize(
            $image->width() * 0.5,
            $image->height() * 0.5,
            function ($constraint) {
                $constraint->aspectRatio();
            }
        );


        $image->save(public_path('images/' . $fielname));
        return back()->with('success', 'Image uploaded successfully.');
    }
}


Blade

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

@if ($message = Session::get('success'))
<div>
    <strong>{{ $message }}</strong>
</div>
@endif

<form action="{{ route('image.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div>
        <input type="file" name="imagea">
        @error('image')
            <div>{{ $message }}</div>
        @enderror
    </div>
    <div>
        <button type="submit">Upload and Resize</button>
    </div>
</form>

</body>
</html>

Lebih baru Lebih lama