Laravel 5.3 multiple image upload and resize

laravel 5.3 upload & resize images

In this post i well show you how to upload multiple images and resize them . first we need to install the required packages using composer .


first go to your project path :

cd path/your/project

than run this commend to install intervention/image package :


composer require intervention/image


add to config/app.php  :

$provides => [
.........
Intervention\Image\ImageServiceProvider::class,
],
$aliases => [
..........
'Image' => Intervention\Image\Facades\Image::class,
]

than in your controller :


 <?php   
   namespace App\Http\Controllers;  
   use App\Http\Controllers\Controller;  
   use Illuminate\Http\Request;  
   use Intervention\Image\Facades\Image;  
   class UploadController extends Controller  
   {  
    
     public function thumbnail(Request $request)  
     {
  
       $this->validate($request, [  
         'image.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg',  
       ]);  

       $uploadPath = public_path("path/to/image/folder"); 

       foreach($request->file('images') as $image)
       {
          $name = md5($image->getFilename().time()).'.'.$image->getClientOriginalExtension();
          $img = Image::make($image->path());
          $img->resize(100,200),function($constraint){
             $constraint->aspectRatio();
          })->save($uploadPath.'/'.$name);
       }
     }  
   }  
 ?> 


That everything good luck ,
if you have any questions or you need to help comment on this post .



Comments

Popular posts from this blog

how to install nginx rtmp monitoring dashboard

How to setup Let's Encrypt Wildcard SSL certificate for Nginx