Advertisement
ozpavel

FilesComponent

Jun 28th, 2020
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.29 KB | None | 0 0
  1. class FilesComponent
  2. {
  3.     private static $maxSize = 1024 * 1024 * 10;
  4.     private static $maxResizeSize = 1024 * 1024 * 1;
  5.     private static $quality = 15;
  6.     private static $error;
  7.  
  8.     public static function uploadImage($file)
  9.     {
  10.         $path = $file['tmp_name'];
  11.         $error = $file['error'];
  12.         $size = $file['size'];
  13.         $name = $file['name'];
  14.         $type = $file['type'];
  15.  
  16.         if (self::checkError($path, $error)) {
  17.  
  18.             if ($size > self::$maxSize) {
  19.                 return ['status' => false, 'error' => 'Файл слишком большой'];
  20.             }
  21.             else {
  22.                 $md5name = md5(time() . $name);
  23.  
  24.                 if (move_uploaded_file($path, HOME . "uploads/" . $md5name)) {
  25.                     self::resize(HOME . "uploads/", $md5name, $size, $type);
  26.                     return ['status' => true, 'filename' => $md5name];
  27.                 }
  28.                 else {
  29.                     return ['status' => false, 'error' => 'Ошибка при загрузке файла!'];
  30.                 }
  31.             }
  32.         }
  33.         else {
  34.             return ['status' => false, 'error' => self::$error];
  35.         }
  36.     }
  37.  
  38.     public static function deleteFile($filename)
  39.     {
  40.         return unlink(HOME . "uploads/{$filename}");
  41.     }
  42.  
  43.     private static function checkError($path, $error)
  44.     {
  45.         if ($error !== UPLOAD_ERR_OK || !is_uploaded_file($path)) {
  46.  
  47.             $errorMessages = [
  48.                 UPLOAD_ERR_INI_SIZE   => 'Файл слишком большой',
  49.                 UPLOAD_ERR_FORM_SIZE  => 'Файл слишком большой',
  50.                 UPLOAD_ERR_PARTIAL    => 'Загружаемый файл повреждён.',
  51.                 UPLOAD_ERR_NO_FILE    => 'Файл не был загружен.',
  52.                 UPLOAD_ERR_NO_TMP_DIR => 'Ошибка #1001',
  53.                 UPLOAD_ERR_CANT_WRITE => 'Не удалось сохранить файл на сервере.',
  54.                 UPLOAD_ERR_EXTENSION  => 'Сервер прервал загрузку файла.',
  55.             ];
  56.  
  57.             $unknownMessage = 'При загрузке файла произошла неизвестная ошибка.';
  58.  
  59.             $outputMessage = isset($errorMessages[$error]) ? $errorMessages[$error] : $unknownMessage;
  60.  
  61.             self::$error = $outputMessage;
  62.  
  63.             return false;
  64.         }
  65.         else {
  66.             return true;
  67.         }
  68.     }
  69.  
  70.     private static function resize($path, $filename, $size, $type)
  71.     {
  72.         if ($size > self::$maxResizeSize) {
  73.             switch($type) {
  74.                 case 'image/jpeg':
  75.                     $source = imagecreatefromjpeg($path . $filename);
  76.                     break;
  77.                 case 'image/png':
  78.                     $source = imagecreatefrompng($path . $filename);
  79.                     break;
  80.                 case 'image/gif':
  81.                     $source = imagecreatefromgif($path . $filename);
  82.                     break;
  83.                 default:
  84.                     return false;
  85.             }
  86.  
  87.             imagejpeg($source, $path . $filename, self::$quality);
  88.             imagedestroy($source);
  89.  
  90.             return true;
  91.         }
  92.         else {
  93.             return false;
  94.         }
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement