Image Optimizing using Intervention Image in laravel

84 0 0 0

Last Updated : 2022-05-23 09:34:19

In this snippet I will teach you how to optimize images using Intervention Image with laravel

In this snippet I will guide you throught all you need to optimize images in laravel using Intervention Image in laravel .

First Step: Install Intervention in your laravel project


1- install intervention using composer like this


composer require intervention/image

2- after you have install intervention image with composer, open config/app.php  and the following lines.

In $providers add this line


Intervention\Image\ImageServiceProvider::class,

In $aliases add this line


'Image' => Intervention\Image\Facades\Image::class,

3- then publish configuration files like this


php artisan config:publish intervention/image

Second Step: configure apache to include gd.

when you get an error like gd not installed in this php version or imagick not installed in this php version 


open xampp then open php.ini search for ;extension:gd then remove semi colon then restart apache .

Third Step: Optimize image in upload in the controller 


you can use the intervention like this


use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;

$name = time().'-'.hexdec(\uniqid()).'.'.$image->getClientOriginalExtension();
$img = Image::make($request->file('image'));
$img->resize(80, 80, function ($constraint) { // change 80*80 to the pixels you want
$constraint->aspectRatio();
})->save(public_path($path . $name));

// OR

Route::get('/', function() {
$img = Image::make('foo.jpg')->resize(300, 200);
return $img->response('jpg');
});

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.