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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay