Intervention Image (Image Manuplation)

 79
Intervention Image (Image Manuplation)

Introduction
Intervention Image is a PHP library for image handling and manipulation. with this library, you can manipulate various aspects of images within your application. It offers features such as image optimization, cropping, resizing, and much more. By leveraging the intervention image library, you can perform a wide range of image related tasks with ease.

Server Requirements

Before beginning the installation, please ensure that your environment meets the following requirements:

  • PHP ≥ 8.1
  • FileInfo Extension
  • One of the below image libraries:
    • GD Library (≥ 2.0) or
    • Imagic PHP Extension (≥ 6.5)

By confirming that your server environment meets these requirements, you can proceed with the installation process smoothly.

Installation

CMD: composer require intervention/image

Create a route

Route::get('/image-upload', [ImageController::class, 'index'])->name('image.create');
Route::post('/image-store', [ImageController::class, 'store'])->name('image.store'); 

Create a controller

Here, we can perform manipulation tasks such as resizing, grayscale, cropping and rotating, efficiently handling various image editing operations.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;

class ImageController extends Controller
{
    public function index()  {
        return view('intervention.create');
    }
    
    public function store(Request $request)  {
       
        $request->validate([
            'image_file' => 'required|image|mimes:png,jpg,jpeg,svg,gif|max:5000'
        ]);
        
        $image = $request->file('image_file');

        // Generate a unique name for the image using the current timestamp and the file's original extension
        $image_name = time().'.'.$image->getClientOriginalExtension();

         // Define the directory path where the original image will be stored
    	$directory = 'uploads/posts/';

        // Ensure the directory exists, if not, create it
        if (!file_exists($directory)) {
            mkdir($directory, 0755, true);
        }

        // Move the uploaded image to the specified directory
        $image->move($directory, $image_name);

        // Create a thumbnail of the uploaded image
        $manager = new ImageManager(new Driver());
        $image = $manager->read($directory.'/'.$image_name);

        // Resize the image to 400x200 pixels for the thumbnail
        $image->resize(400, 200);

        // Ensure the thumbnail directory exists, if not, create it
        $thumbnail_directory = $directory.'thumbnail/';
        if (!file_exists($thumbnail_directory)) {
            mkdir($thumbnail_directory, 0755, true);
        }
        // Save the resized image (thumbnail) in the thumbnail directory
        $image->save(public_path($thumbnail_directory.$image_name));

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

We can compress the image file size using the below command:

$image->toJpeg(10)->save('images/compress_img.png');

Thanks for reading 


Post a Comment

0Comments

* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Post a Comment (0)