Skip to content

Commit

Permalink
Added deleteOriginalFile option. #56 (#58)
Browse files Browse the repository at this point in the history
* Added deleteSourceAfterThumbsGenerating option. #56
  • Loading branch information
Matviy authored and mohorev committed Aug 10, 2018
1 parent 3a10628 commit 946439c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -181,6 +181,7 @@ Behavior Options
* generateNewName - Set true or anonymous function takes the old filename and returns a new name, default value is `true`
* unlinkOnSave - If `true` current attribute file will be deleted, default value is `true`
* unlinkOnDelete - If `true` current attribute file will be deleted after model deletion.
* deleteOriginalFile - Only for UploadImageBehavior. If `true` original image file will be deleted after thumbs generating, default value is `false`.

### Attention!

Expand Down
31 changes: 23 additions & 8 deletions src/UploadImageBehavior.php
Expand Up @@ -59,6 +59,12 @@ class UploadImageBehavior extends UploadBehavior
* @var boolean
*/
public $createThumbsOnRequest = false;
/**
* Whether delete original uploaded image after thumbs generating.
* Defaults to FALSE
* @var boolean
*/
public $deleteOriginalFile = false;
/**
* @var array the thumbnail profiles
* - `width`
Expand Down Expand Up @@ -89,7 +95,7 @@ public function init()

parent::init();

if ($this->createThumbsOnSave) {
if ($this->createThumbsOnSave || $this->createThumbsOnRequest) {
if ($this->thumbPath === null) {
$this->thumbPath = $this->path;
}
Expand All @@ -103,7 +109,7 @@ public function init()
if ($height < 1 && $width < 1) {
throw new InvalidConfigException(sprintf(
'Length of either side of thumb cannot be 0 or negative, current size ' .
'is %sx%s', $width, $height
'is %sx%s', $width, $height
));
}
}
Expand All @@ -127,6 +133,10 @@ protected function afterUpload()
protected function createThumbs()
{
$path = $this->getUploadPath($this->attribute);
if (!is_file($path)) {
return;
}

foreach ($this->thumbs as $profile => $config) {
$thumbPath = $this->getThumbUploadPath($this->attribute, $profile);
if ($thumbPath !== null) {
Expand All @@ -140,6 +150,10 @@ protected function createThumbs()
}
}
}

if ($this->deleteOriginalFile) {
parent::delete($this->attribute);
}
}

/**
Expand All @@ -155,7 +169,7 @@ public function getThumbUploadPath($attribute, $profile = 'thumb', $old = false)
$path = $this->resolvePath($this->thumbPath);
$attribute = ($old === true) ? $model->getOldAttribute($attribute) : $model->$attribute;
$filename = $this->getThumbFileName($attribute, $profile);

return $filename ? Yii::getAlias($path . '/' . $filename) : null;
}

Expand All @@ -168,11 +182,12 @@ public function getThumbUploadUrl($attribute, $profile = 'thumb')
{
/** @var BaseActiveRecord $model */
$model = $this->owner;
$path = $this->getUploadPath($attribute, true);
if (is_file($path)) {
if ($this->createThumbsOnRequest) {
$this->createThumbs();
}

if ($this->createThumbsOnRequest) {
$this->createThumbs();
}

if (is_file($this->getThumbUploadPath($attribute, $profile))) {
$url = $this->resolvePath($this->thumbUrl);
$fileName = $model->getOldAttribute($attribute);
$thumbName = $this->getThumbFileName($fileName, $profile);
Expand Down

0 comments on commit 946439c

Please sign in to comment.