public function getFilterId() { $data = \admin\models\StorageFilter::find()->where(['identifier' => $this->identifier()])->one(); if ($data) { $data->scenario = 'restupdate'; $data->name = $this->name(); $data->update(); return $data->id; } else { // insert new filter $model = new \admin\models\StorageFilter(); $model->scenario = 'restcreate'; $model->attributes = ['name' => $this->name(), 'identifier' => $this->identifier()]; if ($model->save()) { $this->log[] = "created a new filter '" . $this->identifier() . "'."; return $model->id; } } }
public function run() { $this->refresh('thumbnail', ['name' => 'Thumbnail', 'imagine_name' => 'thumbnail', 'imagine_json_params' => json_encode(['vars' => [['var' => 'type', 'label' => 'outbound or inset'], ['var' => 'width', 'label' => 'Breit in Pixel'], ['var' => 'height', 'label' => 'Hoehe in Pixel']]])]); $this->refresh('resize', ['name' => 'Zuschneiden', 'imagine_name' => 'resize', 'imagine_json_params' => json_encode(['vars' => [['var' => 'width', 'label' => 'Breit in Pixel'], ['var' => 'height', 'label' => 'Hoehe in Pixel']]])]); $this->refresh('crop', ['name' => 'Crop', 'imagine_name' => 'crop', 'imagine_json_params' => json_encode(['vars' => [['var' => 'width', 'label' => 'Breit in Pixel'], ['var' => 'height', 'label' => 'Hoehe in Pixel']]])]); $list = []; foreach ($this->getImporter()->getDirectoryFiles('filters') as $file) { $filterClassName = $file['ns']; if (class_exists($filterClassName)) { $object = new $filterClassName(); $object->save(); $list[] = $object->identifier(); $log = $object->getLog(); if (count($log) > 0) { $this->getImporter()->addLog('filters', implode(', ', $log)); } } } foreach (StorageFilter::find()->where(['not in', 'identifier', $list])->all() as $filter) { $filter->delete(); } }
/** * see if the filter for this image already has been applyd, yes return the image_source, otherwise apply * filter and return the new image_source. * * @param int $imageId * @param string $filterIdentifier */ public function filterApply($imageId, $filterIdentifier, $returnImageObject = false) { // resolve $filterIdentifier $filter = StorageFilter::find()->where(['identifier' => $filterIdentifier])->asArray()->one(); if (!$filter) { throw new \Exception('could not find the filterIdentifier ' . $filterIdentifier); } $filterId = $filter['id']; $image = $this->get($imageId); if (!$image) { return false; } $data = (new \yii\db\Query())->from('admin_storage_image')->where(['file_id' => $image->file_id, 'filter_id' => $filterId])->one(); if ($data) { $imageId = $data['id']; } else { $imageId = $this->create($image->file_id, $filterId); } $imageObject = $this->get($imageId); if ($returnImageObject) { return $imageObject; } return $imageObject->source; }
/** * @todo this is a copy of the old code, clean up! * @param unknown $fileId * @param unknown $filterId */ public function addImage($fileId, $filterId = 0, $throwException = false) { try { $query = (new \admin\image\Query())->where(['file_id' => $fileId, 'filter_id' => $filterId])->one(); if ($query && $query->fileExists) { return $query; } $fileQuery = $this->getFile($fileId); if (!$fileQuery) { throw new Exception("Unable to create image, cause the base file does not exist."); } $imagine = new Imagine(); $image = $imagine->open($fileQuery->serverSource); $fileName = $filterId . '_' . $fileQuery->systemFileName; $fileSavePath = $this->serverPath . '/' . $fileName; if (empty($filterId)) { $save = $image->save($fileSavePath); } else { $model = StorageFilter::find()->where(['id' => $filterId])->one(); if (!$model) { throw new Exception("Could not find the provided filter id '{$filterId}'."); } $newimage = $model->applyFilter($image, $imagine); $save = $newimage->save($fileSavePath); } if (!$save) { throw new Exception("unable to store file {$fileSavePath}"); } $resolution = Storage::getImageResolution($fileSavePath); $model = new StorageImage(); $model->setAttributes(['file_id' => $fileId, 'filter_id' => $filterId, 'resolution_width' => $resolution['width'], 'resolution_height' => $resolution['height']]); if (!$model->save()) { throw new Exception("Unable to save storage image, fatal database exception."); } $this->_imagesArray[$model->id] = $model->toArray(); $this->deleteHasCache($this->imageCacheKey); return $this->getImage($model->id); } catch (Exception $err) { if ($throwException) { throw new Exception($err->getMessage(), $err->getCode(), $err); } } return false; }
/** * Find the model based on the identifier. If the identifier does not exists in the database, create * new record in the database. * * @return object \admin\models\StorageFilter */ public function findModel() { // find filter model based on the identifier $model = StorageFilter::find()->where(['identifier' => $this->identifier()])->one(); // if no model exists, create new record if (!$model) { $model = new StorageFilter(); $model->setAttributes(['name' => $this->name(), 'identifier' => $this->identifier()]); $model->insert(false); $this->addLog("added new filter '" . $this->identifier() . "' to database."); } return $model; }