public function applyFilter($image, $imagine) { $newimage = null; $chain = \admin\models\StorageFilterChain::find()->where(['filter_id' => $this->id])->joinWith('effect')->all(); foreach ($chain as $item) { switch ($item->effect->imagine_name) { case 'resize': if (is_null($newimage)) { $newimage = $image->resize(new \Imagine\Image\Box($item->effect_json_values->width, $item->effect_json_values->height)); } else { $newimage = $newimage->resize(new \Imagine\Image\Box($item->effect_json_values->width, $item->effect_json_values->height)); } break; case 'thumbnail': if (is_null($newimage)) { // THUMBNAIL_OUTBOUND & THUMBNAIL_INSET if (!isset($item->effect_json_values->type)) { $item->effect_json_values->type = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; } $newimage = $image->thumbnail(new \Imagine\Image\Box($item->effect_json_values->width, $item->effect_json_values->height), $item->effect_json_values->type); } else { $newimage = $newimage->thumbnail(new \Imagine\Image\Box($item->effect_json_values->width, $item->effect_json_values->height), $item->effect_json_values->type); } break; case 'crop': if (is_null($newimage)) { $newimage = $image->crop(new \Imagine\Image\Point(0, 0), new \Imagine\Image\Box($item->effect_json_values->width, $item->effect_json_values->height)); } else { $newimage = $newimage->crop(new \Imagine\Image\Point(0, 0), new \Imagine\Image\Box($item->effect_json_values->width, $item->effect_json_values->height)); } break; } } return $newimage; }
public function callbackLoadEffects() { $data = \admin\models\StorageFilterChain::find()->where(['filter_id' => $this->getItemId()])->all(); $view = $this->getView()->render('@admin/views/activeWindow/FilterEffectChainList', ['data' => $data]); return $this->response(true, ['html' => $view]); }
/** * Update and save filter corresponding to the model, refresh chain values. * * @return bool */ public function save() { // get the filter model based on the current filter. $filterModel = $this->findModel(); // update the name of the filter if changed if ($filterModel->name !== $this->name()) { $filterModel->setAttribute('name', $this->name()); $filterModel->update(false); $this->addLog("Filter name '" . $this->name() . "' have been updated for identifier '" . $this->identifier() . "'."); } // array containing the processed chain ids $processed = []; $removeImages = false; foreach ($this->getChain() as $chain) { // get filter chain for filter and effect $model = StorageFilterChain::find()->where(['filter_id' => $filterModel->id, 'effect_id' => $chain['effect_id']])->one(); if ($model) { if (Json::decode($chain['effect_json_values']) != $model->effect_json_values) { $model->effect_json_values = $chain['effect_json_values']; if ($model->update(false)) { $removeImages = true; $this->addLog("Effect chain option have been updated for '{$filterModel->name}'."); } } } else { $model = new StorageFilterChain(); $model->setAttributes(['filter_id' => $filterModel->id, 'effect_id' => $chain['effect_id'], 'effect_json_values' => $chain['effect_json_values']]); if ($model->save(false)) { $removeImages = true; $this->addLog("Effect chain option have been added for '{$filterModel->name}'."); } } $processed[] = $model->id; } // remove not used chains for the current filter foreach (StorageFilterChain::find()->where(['not in', 'id', $processed])->andWhere(['=', 'filter_id', $filterModel->id])->all() as $deletion) { $this->addLog("Effect chain option have been removed for '{$filterModel->name}'"); $deletion->delete(); $removeImages = true; } if ($removeImages) { $this->addLog("remove image filter source cache."); $filterModel->removeImageSources(); } return true; }
public function save() { $filterId = $this->getFilterId(); $processed = []; foreach ($this->getChain() as $chain) { $model = \admin\models\StorageFilterChain::find()->where(['filter_id' => $filterId, 'effect_id' => $chain['effect_id']])->one(); if ($model) { $model->effect_json_values = $chain['effect_json_values']; if ($model->save()) { $this->log[] = "updated existing chain value for {$filterId}."; $processed[] = $model->id; } } else { $insert = new \admin\models\StorageFilterChain(); $insert->attributes = ['filter_id' => $filterId, 'effect_id' => $chain['effect_id'], 'effect_json_values' => $chain['effect_json_values']]; if ($insert->save()) { $this->log[] = "insert new chain value for {$filterId} and {$chain['effect_id']}."; $processed[] = $insert->id; } } } $where = []; foreach ($processed as $id) { $where[] = 'id <> ' . $id; } // find old effects $data = \admin\models\StorageFilterChain::find()->where('(' . implode(' AND ', $where) . ') AND filter_id=' . $filterId)->all(); foreach ($data as $deletion) { $this->log[] = "deleted old chain value {$deletion->id}."; $deletion->delete(); } return $this->log; }