Laravel image upload with image compress/resize



To install (Image intervention):
STEP 1 Run

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
 
 
 

Controller :


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Posts;
use Image;
class PostController extends Controller
{
    
    public function create()
    {
        return view('post.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'title'=>'required|max:50',
            'description'=>'nullable|max:200',
            'image'=>'required|image|max:20000',
        ]);
        if($request->hasfile('image'))
     {
          $image=$request->file('image');
        $file_name=time().'.'.$image->getClientOriginalExtension();
    $image_resize = Image::make($image->getRealPath());   
    $image_resize->resize(1280,400);
        $image_resize->save('images/post_images/'.$file_name);
        $data=new Posts;
        $data->image=$file_name;
        $data->title=$request->title;
        $data->description=$request->description;      
        $data->save();
        return redirect()->back()->with('success','Post Add Successfully');


     }
    }

    
   
}
 
 
 

বাসা ভাড়া এখন হাতের মুঠোয়ঃ Basabaribd.com


No comments

Powered by Blogger.