diff --git a/README.md b/README.md index a8281fa..f24cb8f 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,8 @@ Usage Change log ---------- +- **May 13, 2015** - Added "view" action to show image inline instead of forcing dowload. The model has now getDownloadUrl and getViewUrl (maxxer) +- **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/controllers/FileController.php b/controllers/FileController.php index 37e7803..7539a1b 100644 --- a/controllers/FileController.php +++ b/controllers/FileController.php @@ -43,12 +43,59 @@ 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 boolean $crop Optional cropping. Requires yii2-easy-thumbnail-image-helper + * @param boolean $forceDownload If TRUE forces the download of the file, otherwise shows inline + * @return static Image data + */ + public function actionDownload($id, $size = NULL, $crop = FALSE, $forceDownload = TRUE) { + if (!is_null($size)) + $this->checkResizeRequirements (); + $file = File::findOne(['id' => $id]); $filePath = $this->getModule()->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type; + + // Resize if requested + if (!is_null($size)) { + $filePath = \himiklab\thumbnail\EasyThumbnailImage::thumbnailFile( + $filePath, + $size, + $size, + $crop ? \himiklab\thumbnail\EasyThumbnailImage::THUMBNAIL_INSET : \himiklab\thumbnail\EasyThumbnailImage::THUMBNAIL_OUTBOUND); + } - return \Yii::$app->response->sendFile($filePath, "$file->name.$file->type"); + return \Yii::$app->response->sendFile( + $filePath, + $file->name.$file->type, + ['inline' => !$forceDownload, 'mimeType' => $file->mime] + ); + } + + /** + * View 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 boolean $crop Optional cropping. Requires yii2-easy-thumbnail-image-helper + * @return static Image data + */ + public function actionView($id, $size = NULL, $crop = FALSE) + { + $this->actionDownload($id, $size, $crop, FALSE); + } + + /** + * Check if we have the resize functionalities + * @throws Exception + */ + private function checkResizeRequirements () + { + if (!class_exists("\\himiklab\\thumbnail\\EasyThumbnailImage")) { + throw new \Exception("You need himiklab/yii2-easy-thumbnail-image-helper for image resize features"); + } } public function actionDelete($id) diff --git a/models/File.php b/models/File.php index d519dbb..afcfda9 100644 --- a/models/File.php +++ b/models/File.php @@ -18,6 +18,11 @@ * @property integer $size * @property string $type * @property string $mime + * + * @property string $url Alias for downloadUrl + * @property string $downloadUrl Get the download url of the image + * @property string $viewUrl Get the inline view url of the image + * @property string $path Get the local file path of the image */ class File extends ActiveRecord { @@ -60,10 +65,32 @@ public function attributeLabels() ]; } - public function getUrl() + /** + * Obtains the download url of hte image + * @return string + */ + public function getDownloadUrl() { return Url::to(['/attachments/file/download', 'id' => $this->id]); } + + /** + * Obtains the inline view url of hte image + * @return string + */ + public function getViewUrl() + { + return Url::to(['/attachments/file/view', 'id' => $this->id]); + } + + /** + * Support for legacy method + * @return string + */ + public function getUrl() + { + return $this->getDownloadUrl(); + } public function getPath() {