Upload image with GD library First of all I just started learning OOP.

My priority is to make safe code. I think my code is safe, because I use GD to make a copy of the uploaded image and I use an image proxy to make sure nobody can access the uploaded file directly and it's uploaded outside of the webroot. But if I was 100% sure that it's safe I was not posting it here haha. I'm not sure about the way I check the MIME type, and about the GD.

I made some tests and it works fine, but what do you guys think? Any suggestions are welcome. Also if you have any questions about the code just ask me in the comments.

Class Img:

class Img
{
    private static $folder, $file, $ext, $w, $h;

    public static function upload($file, $new_size){
        self::$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
        list(self::$w, self::$h) = ($size = getimagesize($file["tmp_name"]));

        if(!$size || !in_array(self::$ext, ['png', 'jpeg', 'jpg']))die(json_encode(['error' =&gt; 'Invalid file']));
        if(filesize($file["tmp_name"]) &gt; 2700000)die(json_encode(['error' =&gt; 'File is too big']));

        self::$folder = dirname(__DIR__).'/../../uploads/profile/'.$_SESSION['user_folder'];
        self::$file = $file["tmp_name"];

        $_SESSION['avatar'] = ($name = mt_rand(100, 100000).'-'.time().'.'.self::$ext); 
        self::resize_image($name, $new_size, $new_size);

        $user_id = $_SESSION['user_id'];
        $conn = \lib\Db::instance();
        $query = $conn-&gt;query("UPDATE users SET avatar = '{$name}' WHERE user_id = '{$user_id}'");
        $conn = NULL;

        die(json_encode(['avatar' =&gt; $_SESSION['avatar']]));
    }

    private static function resize_image($name, $width, $height){ 
        if(self::$ext === 'jpeg')self::$ext = 'jpg';

        if(self::$ext === 'jpg')$img = imagecreatefromjpeg(self::$file);
        elseif(self::$ext === 'png')$img = imagecreatefrompng(self::$file);

        $ratio = max($width/self::$w, $height/self::$h);
        $x = (self::$w - $width/$ratio)/2;
        self::$h = $height/$ratio;
        self::$w = $width/$ratio;

        $new = imagecreatetruecolor($width, $height);

        // preserve transparency
        if(self::$ext === "png"):
            imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
            imagealphablending($new, false);
            imagesavealpha($new, true);
        endif;

        imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, self::$w, self::$h);

        if(self::$ext === 'jpg')imagejpeg($new, self::$folder.$name);
        elseif(self::$ext === 'png')imagepng($new, self::$folder.$name);
        imagedestroy($new);
    }
}


avatar_upload.php:

require_once dirname(__DIR__).'/../../vendor/autoload.php';

if(!isset($_SESSION))session_start();
if($_SERVER['REQUEST_METHOD'] != 'POST' || empty($_SESSION['user_id']))die(header('HTTP/1.1 404 Not Found'));
if(!empty($_FILES['avatar']['size']))\lib\Img::upload($_FILES["avatar"], 150);
Fight the urge to squeeze multiple "things" into one line.  Your script will be easier to read and maintain with all declarations and constructs occupying their own rightful place.  Spend the extra lines, you'll be happy you did a year from now.
Use curly braces to encapsulate language constructs (e.g. if-else and foreach, etc.), this combined with appropriate tabbing will make your script easier to read and help to prevent mishaps when expressing conditional outcomes.  Start but don't stop reading about coding standards here: https://blog.sideci.com/5-php-coding-standards-you-will-love-and-how-to-use-them-adf6a4855696
Instead of murdering your script with die() calls, return a consistent data type no matter the outcome.  In the future, you may change how you want to display/deliver the results. Finally, convert your returned array to json and echo.
When writing variables into a query, you should be using a prepared statement  with placeholders and bound variables.
Avoid single-use and unnecessary variable declarations.  If you have $_SESSION['avatar'] then you don't need to declare $name.
Starting your session is not a conditional thing.  Do it early and do it every time.
You might like to ask yourself why you've elected to write static methods.  This is a topic worth investing some research time into.  Start, but don't stop here: When should I use static methods?


There is more to tell you, but I have run out of time.