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' => 'Invalid file'])); if(filesize($file["tmp_name"]) > 2700000)die(json_encode(['error' => '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->query("UPDATE users SET avatar = '{$name}' WHERE user_id = '{$user_id}'"); $conn = NULL; die(json_encode(['avatar' => $_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);