Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added image resize options to download #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions components/AttachmentsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
23 changes: 21 additions & 2 deletions controllers/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

}