/**
  * @param $sub
  * @param $module
  * @param $size
  * @param $id
  * @param $baseName
  * @param $extension
  *
  * @return int
  * @throws InvalidConfigException
  * @throws NotFoundHttpException
  * @throws \Exception
  */
 public function actionProcess($sub, $module, $size, $id, $baseName, $extension)
 {
     $directory = (string) floor($id / FPM::getFilesPerDirectory());
     if ($sub !== $directory) {
         throw new NotFoundHttpException(Module::t('exception', 'Wrong generated link'));
     }
     $fileName = FPM::getOriginalDirectory($id) . DIRECTORY_SEPARATOR . FPM::getOriginalFileName($id, $baseName, $extension);
     if (file_exists($fileName)) {
         $data = FPM::transfer()->getData($id);
         if (strtolower($baseName) !== strtolower($data->base_name)) {
             throw new NotFoundHttpException(Module::t('exception', 'File not found'));
         }
         $config = isset(FPM::m()->imageSections[$module][$size]) ? FPM::m()->imageSections[$module][$size] : null;
         if (!is_array($config)) {
             throw new NotFoundHttpException(Module::t('exception', 'Incorrect request'));
         }
         $thumbnailFile = FPM::getThumbnailDirectory($id, $module, $size) . DIRECTORY_SEPARATOR . FPM::getThumbnailFileName($id, $baseName, $extension);
         FileHelper::createDirectory(FPM::getThumbnailDirectory($id, $module, $size));
         if (isset($config['action'])) {
             switch ($config['action']) {
                 case FPM::ACTION_ADAPTIVE_THUMBNAIL:
                     Image::thumbnail($fileName, $config['width'], $config['height'])->save($thumbnailFile)->show($extension);
                     break;
                 case FPM::ACTION_THUMBNAIL:
                     Image::thumbnail($fileName, $config['width'], $config['height'], ManipulatorInterface::THUMBNAIL_INSET)->save($thumbnailFile)->show($extension);
                     break;
                 case FPM::ACTION_CROP:
                     Image::crop($fileName, $config['width'], $config['height'], $config['startX'], $config['startY'])->save($thumbnailFile)->show($extension);
                     break;
                 case FPM::ACTION_CANVAS_THUMBNAIL:
                     Image::canvasThumbnail($fileName, $config['width'], $config['height'])->save($thumbnailFile)->show($extension);
                     break;
                 case FPM::ACTION_FRAME:
                     Image::frame($fileName, 50, 'F00')->save($thumbnailFile)->show($extension);
                     break;
                 case FPM::ACTION_COPY:
                     if (FPM::m()->symLink) {
                         symlink($fileName, $thumbnailFile);
                     } else {
                         copy($fileName, $thumbnailFile);
                     }
                     \Yii::$app->response->sendFile($thumbnailFile);
                     break;
                 default:
                     throw new InvalidConfigException(Module::t('exception', 'Action is incorrect'));
                     break;
             }
         } else {
             throw new InvalidConfigException(Module::t('exception', 'Action not defined'));
         }
     } else {
         throw new NotFoundHttpException(Module::t('exception', 'File not found'));
     }
 }
 /**
  * @param $id
  *
  * @return bool
  * @throws \Exception
  */
 public function deleteFile($id)
 {
     if (!(int) $id) {
         return false;
     }
     $directory = \metalguardian\fileProcessor\helpers\FPM::getOriginalDirectory($id);
     $model = $this->getData($id);
     $fileName = $directory . DIRECTORY_SEPARATOR . \metalguardian\fileProcessor\helpers\FPM::getOriginalFileName($id, $model->base_name, $model->extension);
     if (is_file($fileName)) {
         $result = unlink($fileName) && $this->deleteData($id) ? true : false;
     } else {
         $result = false;
     }
     return $result;
 }
Beispiel #3
0
 /**
  * @return string
  */
 public function actionSaveCroppedImage()
 {
     $data = \Yii::$app->request->post('data');
     $data = $data ? Json::decode($data) : null;
     if ($data) {
         $fileId = $data['fileId'];
         $imageEntity = EntityToFile::find()->where('file_id = :id', [':id' => (int) $fileId])->one();
         if ($imageEntity) {
             //Find original img path
             $directory = FPM::getOriginalDirectory($imageEntity->file_id);
             FileHelper::createDirectory($directory, 0777, true);
             $fileName = $directory . DIRECTORY_SEPARATOR . FPM::getOriginalFileName($imageEntity->file_id, $imageEntity->file->base_name, $imageEntity->file->extension);
             //Delete cached image
             FPM::cache()->delete($imageEntity->file_id);
             //Delete thumbs
             $this->clearImageThumbs($imageEntity->file);
             Image::crop($fileName, $data['width'], $data['height'], $data['startX'], $data['startY'])->save($fileName);
             return Json::encode(['replaces' => [['what' => '#preview-image-' . $imageEntity->file_id, 'data' => Html::img(FPM::originalSrc($imageEntity->file_id) . '?' . time(), ['class' => 'file-preview-image', 'id' => 'preview-image-' . $imageEntity->file_id])]], 'js' => Html::script('hideModal(".modal")')]);
         }
     }
 }