resize image and store it as thumbnail as well as store real image in laravel + resize image and store in laravel
Reference site 1: https://itsolutionstuff.com/post/laravel-5-image-upload-and-resize-example-using-intervention-image-packageexample.html
i.e. intervention/image in laravel
Main reference site is :
http://image.intervention.io/
when any error ocurs during integration then follow :
https://laracasts.com/discuss/channels/laravel/some-problems-with-install-intervention-image?page=1
INSIDE CONTROLLER :
In store funtion and update function:
Lets see next way to store image after resizing in laravel:
Reference :
https://stackoverflow.com/questions/40358510/resize-image-in-laravel-5-2?fbclid=IwAR3bO_etiRph2qrh4LaTCvPKMLGJWJ3W0pQkJU3Q-13tJw8bG7ZEqPhtynM
here , we use Intervention package to do so,
i.e. intervention/image in laravel
Main reference site is :
http://image.intervention.io/
when any error ocurs during integration then follow :
https://laracasts.com/discuss/channels/laravel/some-problems-with-install-intervention-image?page=1
INSIDE CONTROLLER :
use Intervention\Image\Facades\Image;
In store funtion and update function:
if(!empty($request->file('image'))){
$file =$request->file('image');
// dd($file);
$path=base_path().'/public/multimedia_upload';
$thumbpath=public_path('/multimedia_upload');
// this above is folder name inside public folder where we store thubnail
$img = Image::make($file->getRealPath())->resize(100, 100,
function ($constraint) {$constraint->aspectRatio();});
function ($constraint) {$constraint->aspectRatio();});
$name=uniqid().'_'.$file->getClientOriginalName();
$thumbname=uniqid().'_'.$file->getClientOriginalName();
if($file->move($path,$name)){
$image=$name;
$input['image']=$image;
}
if($img->save($thumbpath.'/'.$thumbname))
//above code is to store image inside folder created above along with the name created above
{
$thumbimage=$thumbname;
$input['other_file']=$thumbimage;
}
}
Lets see next way to store image after resizing in laravel:
Reference :
https://stackoverflow.com/questions/40358510/resize-image-in-laravel-5-2?fbclid=IwAR3bO_etiRph2qrh4LaTCvPKMLGJWJ3W0pQkJU3Q-13tJw8bG7ZEqPhtynM
here , we use Intervention package to do so,
composer require intervention/image
STEP 2 On your config/app.php:
In the $providers array, add the following:
Intervention\Image\ImageServiceProvider::class
In the $aliases array,add the following:
'Image' => Intervention\Image\Facades\Image::class
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLantervention\Image\ImageServiceProviderLaravel5"
STEP 3 On top of your controller
use Intervention\Image\ImageManagerStatic as Image;
STEP 4 On your method (there are several ways but this will give you an idea)
public function store(Request $request)
{
$input=$request->all();
$image='';
if(!empty($request->file('image'))){
$file =$request->file('image');
$path=base_path().'/public/success_upload/';
$name=uniqid().'_'.$file->getClientOriginalName();
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
if($image_resize->save($path.$name)){
$image=$name;
$input['image']=$image;
}
use Intervention\Image\Facades\Image;
This is approved one i have done
$image = '';
if (!empty($request->file('image'))) {
$file = $request->file('image');
$path = public_path() . '/advertisement_image/';
if (!file_exists($path)) {
mkdir($path, 666, true);
}
$name = uniqid() . '_' . $file->getClientOriginalName();
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
if ($image_resize->save($path . $name)) {
$image = $name;
$input['image'] = $image;
}
}
}
$status=success::create($input);
if($status){
Session::flash('success','Information added successfully.');
}else{
Session::flash('error','Information cannot be added.');
}
return redirect('backend/success');
}
Comments
Post a Comment