コード例 #1
0
 public function changeAvatar($userId)
 {
     $user = UserRepository::findUserById($userId);
     if (!$user) {
         $this->sendResponse(array('success' => false, 'message' => 'User is not existing!'));
     }
     if (empty($_FILES)) {
         $this->sendResponse(array('success' => false, 'message' => 'No file found!'));
     }
     $file = $_FILES["avatar"];
     if ($file['error'] > 0) {
         $this->sendResponse(array('success' => false, 'message' => $file['error']));
     }
     $fileName = $file['name'];
     $hashFileName = base64_encode(date('Ymdhis') . uniqid());
     $temp = explode('.', $file['name']);
     $extension = end($temp);
     if (!Validation::isValidImage($file)) {
         $this->sendResponse(array('success' => false, 'message' => 'File type is invalid!'));
     }
     $avatarFolder = $this->app->config['app']['avatar_path'] . DS . $userId;
     $fs = new Filesystem();
     try {
         $fs->mkdir($avatarFolder);
     } catch (IOExceptionInterface $e) {
         $this->app->log->getWriter()->write('An error occurred while creating your directory at ' . $e->getPath(), \Slim\Log::ERROR);
     }
     if ($fs->exists($avatarFolder)) {
         $fileTmpPath = $file['tmp_name'];
         $fileTargetPath = implode(DS, array($this->app->config['app']['avatar_path'], $userId, $hashFileName . '.' . $extension));
         move_uploaded_file($fileTmpPath, $fileTargetPath);
         $imagine = new Imagine();
         $image = $imagine->open($fileTargetPath);
         $image->thumbnail(new Box(180, 180))->save($fileTargetPath);
         $data = array('AvatarUrl' => implode('/', array($this->app->config['app']['asset_base_url'], 'avatar', $userId, $hashFileName . '.' . $extension)), 'LastModifiedDate' => date('Y-m-d H:i:s'));
         $success = UserRepository::update($user['UserId'], $data);
         if (!$success) {
             $this->sendResponse(array('success' => false, 'message' => 'Operation failed!'));
         }
         $this->sendResponse(array('success' => true, 'url' => $data['AvatarUrl']));
     } else {
         $this->sendResponse(array('success' => false, 'message' => 'Operation failed!'));
     }
 }
コード例 #2
0
 private function processUploadedFile($fileInputName, $type)
 {
     if (empty($_FILES) || !isset($_FILES[$fileInputName])) {
         self::sendResponse(array('success' => false, 'message' => 'No file found!'));
     }
     $file = $_FILES[$fileInputName];
     if ($file['error'] > 0) {
         self::sendResponse(array('success' => false, 'message' => $file['error']));
     }
     if ($type == 'image' && !Validation::isValidImage($file) || $type == 'video' && !Validation::isValidVideo($file) || $type == 'audio' && !Validation::isValidAudio($file)) {
         self::sendResponse(array('success' => false, 'message' => 'File type is invalid!'));
     }
     $temp = explode('.', $file['name']);
     $targetFolder = implode(DS, array($this->app->config['app']['media_path'], date('Y'), date('m'), date('d')));
     $fs = new Filesystem();
     try {
         $fs->mkdir($targetFolder);
     } catch (IOExceptionInterface $e) {
         $this->app->log->getWriter()->write('An error occurred while creating your directory at ' . $e->getPath(), \Slim\Log::ERROR);
     }
     if (!$fs->exists($targetFolder)) {
         $this->sendOperationFailedResult('File');
     }
     $hashFileName = base64_encode(date('Ymdhis') . uniqid());
     $rtn = array('Extention' => end($temp), 'RawFileName' => $file['name'], 'HashFileName' => $hashFileName, 'FileTmpPath' => $file['tmp_name'], 'Url' => implode('/', array($this->app->config['app']['asset_base_url'], 'media', date('Y'), date('m'), date('d'), $hashFileName . '.' . end($temp))));
     $fileTargetPath = implode(DS, array($this->app->config['app']['media_path'], date('Y'), date('m'), date('d'), $rtn['HashFileName'] . '.' . $rtn['Extention']));
     move_uploaded_file($rtn['FileTmpPath'], $fileTargetPath);
     if ($type == 'image') {
         $imagine = new Imagine();
         $image = $imagine->open($fileTargetPath);
         $image->thumbnail(new Box(180, 180))->save(str_replace('.' . $rtn['Extention'], '_180x180.' . $rtn['Extention'], $fileTargetPath));
         $rtn['ThumbUrl'] = implode('/', array($this->app->config['app']['asset_base_url'], 'media', date('Y'), date('m'), date('d'), $rtn['HashFileName'] . '_180x180.' . $rtn['Extention']));
     }
     return $rtn;
 }