Skip to content

Commit

Permalink
add support for thumbnail background alpha (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
rinu authored Feb 15, 2022
1 parent ebc687a commit 2e43329
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/UploadImageBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected function createThumbs()
if (!is_file($path)) {
return;
}

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

if ($this->deleteOriginalFile) {
parent::delete($this->attribute);
}
Expand All @@ -169,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 @@ -182,11 +182,11 @@ public function getThumbUploadUrl($attribute, $profile = 'thumb')
{
/** @var BaseActiveRecord $model */
$model = $this->owner;

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

if (is_file($this->getThumbUploadPath($attribute, $profile))) {
$url = $this->resolvePath($this->thumbUrl);
$fileName = $model->getOldAttribute($attribute);
Expand Down Expand Up @@ -257,6 +257,7 @@ protected function generateImageThumb($config, $path, $thumbPath)
$quality = ArrayHelper::getValue($config, 'quality', 100);
$mode = ArrayHelper::getValue($config, 'mode', ManipulatorInterface::THUMBNAIL_INSET);
$bg_color = ArrayHelper::getValue($config, 'bg_color', 'FFF');
$bg_alpha = ArrayHelper::getValue($config, 'bg_alpha', 100);

if (!$width || !$height) {
$image = Image::getImagine()->open($path);
Expand All @@ -271,6 +272,7 @@ protected function generateImageThumb($config, $path, $thumbPath)
// Fix error "PHP GD Allowed memory size exhausted".
ini_set('memory_limit', '512M');
Image::$thumbnailBackgroundColor = $bg_color;
Image::$thumbnailBackgroundAlpha = $bg_alpha;
Image::thumbnail($path, $width, $height, $mode)->save($thumbPath, ['quality' => $quality]);
}
}

0 comments on commit 2e43329

Please sign in to comment.