<?php

namespace App\Http\HelperClasses;

use App\Models\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use phpDocumentor\Reflection\Types\Self_;

class UploadFile
{
    protected static $file;
    protected static $fileName;
    protected static $typeFile;
    protected static $catType;
    protected static $objId;
    protected static $mimeType;
    protected static $urlResizeImage = [];

    public static function format()
    {
        return [
            &#039;images&#039; => [&#039;jpeg&#039;, &#039;jpg&#039;, &#039;png&#039;, &#039;bmp&#039;, &#039;gif&#039;, &#039;jfif&#039;],
            &#039;videos&#039; => [&#039;mp4&#039;, &#039;mkv&#039;, &#039;flv&#039;, &#039;mov&#039;, &#039;avi&#039;, &#039;wmv&#039;],
            &#039;voices&#039; => [&#039;mp3&#039;],
            &#039;documents&#039; => [&#039;pdf&#039;, &#039;doc&#039;, &#039;pptx&#039;, &#039;xls&#039;],
            &#039;compress&#039; => [&#039;zip&#039;, &#039;rar&#039;],
        ];
    }

    public static function resizeable()
    {
        return [
            &#039;jpg&#039;, &#039;jpeg&#039;, &#039;png&#039;, &#039;bmp&#039;
        ];
    }

    public static function size()
    {
        return [
            [200, 200],
            [600, null]
        ];
    }

    public static function route()
    {
        return "upload/" . jdate()->format(&#039;Y&#039;) . "/" . jdate()->format(&#039;m&#039;) . "/" . self::$typeFile . "/" . strtolower(self::$catType)."s" . "/" . self::$objId;
    }


    public static function upload($file,$cat,$queid)
    {
        self::directoryType($file->getClientOriginalExtension());
        self::$mimeType = $file->getMimeType();
        self::$catType = $cat;
        self::$objId = $queid;
        $fileName = self::checkExistFile($file->getClientOriginalName());

        if (in_array($file->getClientOriginalExtension(), self::resizeable())) {
            $file->storeAs(self::route(), $fileName);
            self::resize($file, $fileName);
        }
        $file->storeAs(self::route(), $fileName);
        $file_name_hash = md5($fileName);
        return [
            "path" =>
                array_merge(self::$urlResizeImage, [[
                    "upf_path" => self::route() . $fileName,
                    "upf_dimension" => "fullsize"
                ]]),
            "option" => [
                "upf_object_id" => "",
                "upf_object_type_id" => "",
                "upf_type" => self::$typeFile,
                "upf_category" => self::$catType,
                "upf_mime_type" => self::$mimeType,
                &#039;upf_file_name&#039; => $fileName,
                &#039;upf_file_hash&#039; => $file_name_hash,
            ]
        ];
    }

    public static function directoryType($typeFile)
    {
        foreach (Self::format() as $key => $value) {
            if (in_array($typeFile, $value)){
                self::$typeFile = $key;
                break;
            }else{
                self::$typeFile = "other";
            }
        }
        return self::$typeFile;
    }

    public static function checkExistFile($fileName)
    {
        $pathFile = storage_path("app/public/" . self::route() . $fileName);

        if (File::exists($pathFile))
            return self::checkExistFile(time() . $fileName);
        else
            return $fileName;
    }

    public static function resize($file, $fileName)
    {
        $path = &#039;questions&#039;;
        foreach (self::size() as $key => $value) {
            $resizePath = self::route() . "{$value[0]}x{$value[1]}_" . $fileName;
            Image::make($file->getRealPath())
                ->resize($value[0], $value[1], function ($constraint) {
                    $constraint->aspectRatio();
                })
                ->save(storage_path("app/public/" . $path));
            $urlResizeImage[] = ["upf_path" => $resizePath, "upf_dimension" => "{$value[0]}x{$value[1]}"];
        }
        self::$urlResizeImage = $urlResizeImage;
    }
}

Add a code snippet to your website: www.paste.org