Skip to main content

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 :



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();});
$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

Popular posts from this blog

Installing Admin LTE in Laravel

step 1:  Reference---  https://hdtuto.com/article/laravel-56-adminlte-bootstrap-theme-installation-example Step 2 : after completion first reference view this link:    https://github.com/JeroenNoten/Laravel-AdminLTE step 3:  For more additional information view this link:   https://github.com/jeroennoten/Laravel-AdminLTE#2-updating Now you are done statically : https://adminlte.io/blog/integrate-adminlte-with-laravel Steps: 1:  composer require jeroennoten/laravel-adminlte 2: 'providers' => [ .... JeroenNoten\LaravelAdminLte\ServiceProvider::class, ], 3: php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\ServiceProvider" --tag=assets 5: php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\ServiceProvider" --tag=config 6: php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\ServiceProvider" --tag=views 7: for admin pannel php artisan ...

Integration of adminlte admin pannel in laravel 6 adn above

Create new laravel project : laravel new projectname --auth Steps to integrate admin pannel i.e. adminlte 1.  composer require jeroennoten/laravel-adminlte 2.  php artisan adminlte:install 3.  php artisan adminlte:install --only=auth_views 4. php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\AdminLteServiceProvider" --tag=views Filemanager: https://unisharp.github.io/laravel-filemanager/installation 1.  composer require unisharp/laravel-filemanager:~1.8 php artisan vendor:publish --tag = lfm_config php artisan vendor:publish --tag = lfm_public 3.  php artisan route:clear php artisan config:clear you will get a problem regarding file relode after upload and to solve do this fix it on vendor\unisharp\laravel-filemanager\src\Controllers\UploadController.php replace line 60 return count($this->errors) > 0 ? $this->errors : parent::$success_response; by return count($this->errors) > 0 ? ...

Setting and Getting Cookie and Session in laravel

First of all lets discuss many ways to set cookies inside controller : $response = new Response ( redirect ( 'news' )); //$minute=5; // $response->withCookie(cookie('name', 'virat', $minutes)); $response -> withCookie ( cookie ()-> forever ( 'news_id' , $newsid )); return $response ; // or // Cookie::queue('news_id', $news_id, 15); // return redirect('news'); // or // return redirect('news')->withCookie(cookie('name', 'virat', $minutes)); use Illuminate\Support\Facades\ Request ; use Illuminate\Http\ Response ; use Illuminate\Support\Facades\ Cookie ; To get cookies: inside controller $val = Request :: cookie ( 'news_id' ); // dd($val); $v = Cookie :: get ( 'news_id' ); // dd($v); or $value = $request -> cookie ( 'name' ); in blade: {{ Cookie :: get ( 'news_id' ) }} Now lets see for session: Ref...