Install and Run Image Intervention in Laravel

Install and Run Image Intervention in Laravel

When presenting version 3, the Image Intervention package has seen many changes. codes version 2 in version 3 Not compatible and It needs to be changed.

okey.

Install Package Image Intervention :

composer require intervention/image

in Database users, create table image :

$table->string(‘image’)->nullable();

Creating Field image :

<div class=”col-12″>
<label for=”input8″ class=”form-label”>Upload image</label>
<input type=”file” name=”image” class=”form-control rounded-5 @error(‘image’) is-invalid @enderror” id=”input8″>
@error(‘image’)
<span class=”invalid-feedback” role=”alert”>
<strong>{{ $message }}</strong>
</span>
@enderror
</div>

in windows xampp extension=imagick.so enable. in linux Enter the following commands :

sudo apt install php-imagick
php -m | grep imagick
sudo systemctl restart apache2

Creating Function saveImage in User.php :

use InterventionImageImageManager;

public static function saveImage($file)
{
if ($file){
$name = $file->hashName();

$smallImage = ImageManager::imagick()->read($file->getRealPath());
$bigImage = ImageManager::imagick()->read($file->getRealPath());
$smallImage->resize(256, 256, function ($constraint){
$constraint->aspectRatio();
});

Storage::disk(‘local’)->put(‘users/small/’.$name, (string) $smallImage->encodeByMediaType(‘image/jpeg’, 90));
Storage::disk(‘local’)->put(‘users/big/’.$name, (string) $bigImage->encodeByMediaType(‘image/jpeg’, 90));

return $name;

}else{
return “”;
}
}

Function Store in UserController.php :

public function store(Request $request)
{
$image = User::saveImage($request->image);

User::query()->create([
‘name’ => $request->name,
’email’ => $request->email,
‘password’ => bcrypt($request->password),
‘image’ => $image,
]);

return to_route(‘users.index’);
}

in config/filesystem.php Enter the following codes :

‘local’ =>
[
‘driver’ => ‘local’,
‘root’ => public_path(‘images’),
‘throw’ => false,
],

Show Image User :

@foreach($users as $index=>$row)
<figure>
<img src=”{{asset(‘images/users/small/’ .$row->image)}}” class=”rounded-4″ width=”52px”>
</figure>
@endforeach

Good luck. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *