diff --git a/Module.php b/Module.php index c0ebceb..c78f72c 100644 --- a/Module.php +++ b/Module.php @@ -168,4 +168,13 @@ public function detachFile($id) $file->delete(); } + + /** + * Check if we have the resize functionalities + * @throws Exception + */ + public function checkResizeRequirements () + { + return class_exists("\\himiklab\\thumbnail\\EasyThumbnailImage"); + } } diff --git a/README.md b/README.md index a8281fa..06f57b0 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ Installation 6. Make sure that you specified `maxFiles` in module rules and `maxFileCount` on `AttachmentsInput` to the number that you want +7. If you want to use resize features install [himiklab/yii2-easy-thumbnail-image-helper](https://github.com/himiklab/yii2-easy-thumbnail-image-helper) + Usage ----- @@ -113,6 +115,7 @@ Usage Change log ---------- +- **May 13, 2015** - Added optional image resize, using himiklab/yii2-easy-thumbnail-image-helper (maxxer) - **May 1, 2015** - Fixed uploading when connection is slow or uploading time is long. Now ```onclick``` event on submit button is deprecated - **Apr 16, 2015** - Allow users to have a custom behavior class inheriting from FileBehavior. - **Apr 4, 2015** - Now all temp uploaded files will be deleted on every new form opened. diff --git a/components/AttachmentsTable.php b/components/AttachmentsTable.php index 6ecaa4f..b8312f9 100644 --- a/components/AttachmentsTable.php +++ b/components/AttachmentsTable.php @@ -71,6 +71,17 @@ public function run() [ 'class' => 'yii\grid\SerialColumn' ], + [ + 'label' => $this->getModule()->t('attachments', 'Preview'), + 'format' => 'raw', + 'value' => function ($model) { + return $model->isImage ? Html::a(\himiklab\thumbnail\EasyThumbnailImage::thumbnailImg( + $model->path, 150, 150, + \himiklab\thumbnail\EasyThumbnailImage::THUMBNAIL_OUTBOUND), + $model->getViewUrl(), ['target' => '_blank']) : ""; + }, + 'visible' => $this->getModule()->checkResizeRequirements(), + ], [ 'label' => $this->getModule()->t('attachments', 'File name'), 'format' => 'raw', diff --git a/controllers/FileController.php b/controllers/FileController.php index 37e7803..dd39a94 100644 --- a/controllers/FileController.php +++ b/controllers/FileController.php @@ -43,14 +43,33 @@ public function actionUpload() } } - public function actionDownload($id) + /** + * Download action for the file + * @param integer $id ID of the image to fetch + * @param integer $size Optional maximum size of the image. Requires yii2-easy-thumbnail-image-helper + * @param bool $crop Optional cropping. Requires yii2-easy-thumbnail-image-helper + * @return static Image data + */ + public function actionDownload($id, $size = NULL, $crop = FALSE) { + if (!is_null($size) && $this->getModule()->checkResizeRequirements() == FALSE) + throw new \Exception("You need himiklab/yii2-easy-thumbnail-image-helper for image resize features"); + $file = File::findOne(['id' => $id]); $filePath = $this->getModule()->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type; + + // Resize if requested + if (!is_null($size) && $file->isImage) { + $filePath = \himiklab\thumbnail\EasyThumbnailImage::thumbnailFile( + $filePath, + $size, + $size, + $crop ? \himiklab\thumbnail\EasyThumbnailImage::THUMBNAIL_OUTBOUND : \himiklab\thumbnail\EasyThumbnailImage::THUMBNAIL_INSET); + } return \Yii::$app->response->sendFile($filePath, "$file->name.$file->type"); } - + public function actionDelete($id) { $this->getModule()->detachFile($id); diff --git a/models/File.php b/models/File.php index d519dbb..9d38a18 100644 --- a/models/File.php +++ b/models/File.php @@ -18,6 +18,8 @@ * @property integer $size * @property string $type * @property string $mime + * + * @property boolean $isImage Returns TRUE if the file's mime type is image/* */ class File extends ActiveRecord { @@ -69,4 +71,10 @@ public function getPath() { return $this->getModule()->getFilesDirPath($this->hash) . DIRECTORY_SEPARATOR . $this->hash . '.' . $this->type; } + + public function getIsImage() + { + return preg_match('/image\/.*/', $this->mime); + } + }