<?php

namespace App\Http\Controllers;

use App\Models\Users;
use App\Models\MOfferwall;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use File,Image;


class Fun extends Controller
{
    public static function StoreImage($location,$width,$height,$req,$filename,$update){
        $image = $req->icon;
        $filenameWithExt = $image->getClientOriginalName();
        $extension = $image->getClientOriginalExtension();
        $fileNameToStore = uniqid() . '_' . time() . '.' . $extension;
        $image_resize = Image::make($image->getRealPath());
        if($width!=null||$height!=null){
           $image_resize->resize($width,$height);
        }
        $save = $image_resize->save('images/' . $fileNameToStore);
        if($update && $req->oldicon!=null){
            
            if($location=="images"){
               $image_path = "/images/" . $req->oldicon; 
            }
            
            if($image_path!=null){
                if (File::exists($image_path)) {
                    File::delete($image_path);
                }
            }
        }
        
        if($save){
            return $fileNameToStore;
        }else{
            return null;
        }
    }
    
    public function RemoveImage($path,$image){
        $image_path = $path.$image; 
        if($image_path!=null){
            if (File::exists($image_path)) {
                File::delete($image_path);
            }
        }
    }
    
    
}