DEV Community

Md Abdullah al noor
Md Abdullah al noor

Posted on

Upload svg and other format image laravel intervention

Upload svg and other format image laravel intervention

use App\SiteSetting;
use Session;
use Image;
use DB;

$this->validate($request,[
'site_logo' => 'required|mimes:png,jpeg,svg',
]);

DB::beginTransaction();

try{
$siteSetting = SiteSetting::firstOrFail();
$siteSetting->site_logo_title = $request->site_logo_title;
if($request->hasFile('site_logo')){

         if($siteSetting->site_logo){
             if(substr($siteSetting->site_logo, -3)  == 'svg'){
                @unlink(public_path().'/'.$siteSetting->site_logo);
             }else{
                  $this->globalImageUnlink($siteSetting->site_logo);
             }
        }

        $originalImage = $request->file('site_logo');
        $imagePath = 'frontend/images/site-images/';
        $imageName = uniqid().time().'logo.'.$originalImage->getClientOriginalExtension();
        $imageFullPath = $imagePath.$imageName;

        if($originalImage->getClientOriginalExtension() == 'svg'){
            $originalImage->move(public_path().'/'.$imagePath,$imageName);
        }else{
            $image = Image::make($originalImage);
            $this->globalImageSave($image,$imageFullPath);
        }

        $siteSetting->site_logo =  $imageFullPath;
    }

        $siteSetting->save();

        DB::commit();
        $status = true;

    }catch(\Exception  $e){
       return $message = $e->getMessage();
        DB::rollback();
        $status = false;
        return back()->with('error','Please fill out form correctly...');
    }


    Session::flash('success','Information Saved Successfully..');
    return \redirect()->route('admin.edit.setting');
Enter fullscreen mode Exit fullscreen mode

protected function globalImageSave($image,$imageFullPath){
$image->save(public_path().'/'.$imageFullPath);
}
protected function globalImageUnlink($imagePath){
@unlink(public_path().'/'.$imagePath);
}

Top comments (0)