/**
  * @param $model
  * @param $type
  * @param $id
  * @param $fileName
  * @param $ext
  *
  * @throws \CHttpException
  */
 public function actionResize($model, $type, $id, $fileName, $ext)
 {
     $file = FPM::getOriginalFilePath($id, $fileName, $ext);
     if (file_exists($file)) {
         $meta = FPM::transfer()->getMetaData($id);
         if (!(is_array($meta) && $fileName === $meta['real_name'])) {
             throw new CHttpException(404, 'File not found');
         }
         $config = isset(FPM::m()->imageSections[$model]) && isset(FPM::m()->imageSections[$model][$type]) ? FPM::m()->imageSections[$model][$type] : null;
         if (!$config) {
             throw new CHttpException(400, 'Incorrect request');
         }
         $thumbFile = FPM::getCachedImagePath($id, $model, $type, $id . '-' . $meta['real_name'] . '.' . $meta['extension']);
         FPM::createCacheDir($id, $model, $type);
         /** @var $ih \fileProcessor\extensions\imageHandler\drivers\MDriverAbstract|\fileProcessor\extensions\imageHandler\MImageHandler */
         $ih = Yii::createComponent(FPM::m()->getImageHandler());
         $ih->init();
         $ih->load($file);
         if (isset($config['do'])) {
             switch ($config['do']) {
                 case 'adaptiveResize':
                     $ih->adaptiveThumb($config['width'], $config['height']);
                     break;
                 case 'resize':
                     $ih->resize($config['width'], $config['height']);
                     break;
                 case 'copy':
                     // do nothing, create copy of the file
                     break;
                 case 'resizeCanvas':
                     $ih->resizeCanvas($config['width'], $config['height']);
                     break;
                 case 'rhombusResizeCanvas':
                     $ih->resizeCanvas(floor($config['width'] / 2), floor($config['height'] / 2))->resizeCanvas($config['width'], $config['height']);
                     break;
                 default:
                     throw new CHttpException(400, 'Incorrect action');
                     break;
             }
         } else {
             $ih->adaptiveThumb($config['width'], $config['height']);
         }
         $ih->save($thumbFile, false, $config['quality']);
         $ih->show(false, $config['quality']);
     } else {
         throw new CHttpException(404, 'File not found');
     }
     Yii::app()->end();
 }
 /**
  * Delete cached images.
  *
  * @param integer $id image id
  *
  * @internal param bool|string $ext
  *
  * @return void
  */
 public function delete($id)
 {
     $metaData = FPM::transfer()->getMetaData($id);
     if (!in_array($metaData['extension'], array('png', 'jpeg', 'jpg', 'gif'), true)) {
         return;
     }
     $config = FPM::m()->imageSections;
     foreach ($config as $modelKey => $model) {
         foreach ($model as $typeKey => $type) {
             $fileName = FPM::getCachedImagePath($id, $modelKey, $typeKey, $metaData['real_name']);
             if (is_file($fileName)) {
                 unlink($fileName);
             }
         }
     }
 }
 /**
  * Delete file
  *
  * @param integer $id file id
  *
  * @return boolean
  */
 public function deleteFile($id)
 {
     if (!(int) $id) {
         return false;
     }
     $dirName = $this->getBaseDestinationDir() . DIRECTORY_SEPARATOR . floor($id / $this->getMaxFilesPerDir());
     $meta = FPM::transfer()->getMetaData($id);
     $fileName = $dirName . DIRECTORY_SEPARATOR . $id . '-' . $meta['real_name'] . '.' . $meta['extension'];
     if (is_file($fileName)) {
         $result = unlink($fileName) && $this->deleteMetaData($id) ? true : false;
     } else {
         $result = false;
     }
     return $result;
 }
 /**
  * @param CModelEvent $event
  *
  * @return bool|void
  */
 public function beforeSave($event)
 {
     /** @var $owner ActiveRecord */
     $owner = $this->getOwner();
     if (!$owner->isNewRecord && in_array($owner->getScenario(), $this->scenarios, true) && ($files = CUploadedFile::getInstances($owner, 'fileUploader')) && !empty($files)) {
         /** @var CUploadedFile[] $files */
         foreach ($files as $file) {
             $fileId = FPM::transfer()->saveUploadedFile($file);
             if ($this->hasEventHandler('onSaveImage')) {
                 $event = new CEvent($this);
                 $event->params = array('fileId' => $fileId);
                 $this->onSaveImage($event);
             }
             FPM::m()->getDb()->createCommand()->insert(FPM::m()->relatedTableName, array('file_id' => $fileId, 'model_id' => $owner->getPrimaryKey(), 'model_class' => $owner->getClassName()));
         }
     }
 }
 /**
  * @param CModelEvent $event
  *
  * @return bool|void
  */
 public function beforeSave($event)
 {
     /** @var $owner CActiveRecord */
     $owner = $this->getOwner();
     if (in_array($owner->getScenario(), $this->scenarios, true) && ($file = CUploadedFile::getInstance($owner, $this->attributeName))) {
         // delete old file
         $this->deleteFile();
         $fileId = FPM::transfer()->saveUploadedFile($file);
         if ($this->hasEventHandler('onSaveImage')) {
             $event = new CEvent($this);
             $event->params = array('fileId' => $fileId);
             $this->onSaveImage($event);
         }
         $owner->{$this->attributeName} = $fileId;
     }
 }
Ejemplo n.º 6
0
 /**
  * @param $id
  *
  * @return null
  */
 public static function deleteFiles($id)
 {
     if (!(int) $id) {
         return null;
     }
     FPM::cache()->delete($id);
     FPM::transfer()->deleteFile($id);
     return true;
 }