public function run()
 {
     $this->refresh('thumbnail', ['name' => 'Thumbnail', 'imagine_name' => 'thumbnail', 'imagine_json_params' => json_encode(['vars' => [['var' => 'width', 'label' => 'Breit in Pixel'], ['var' => 'height', 'label' => 'Hoehe in Pixel'], ['var' => 'mode', 'label' => 'outbound or inset'], ['var' => 'saveOptions', 'label' => 'save options']]])]);
     /*
     $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'],
             ['var' => 'saveOptions', 'label' => 'save options'],
         ]]),
     ]);
     */
     $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'], ['var' => 'saveOptions', 'label' => 'save options']]])]);
     $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->addLog(implode(', ', $log));
             }
         }
     }
     foreach (StorageFilter::find()->where(['not in', 'identifier', $list])->all() as $filter) {
         $filter->delete();
     }
 }
 /**
  * Add a new image based an existing file Id.
  *
  * The storage system uses the same file base, for images and files. The difference between a file and an image is the filter which is applied.
  *
  * Only files of the type image can be used (or added) as an image.
  *
  * An image object is always based on the {{\luya\admin\file\Item}} object and a {{luya\admin\base\Filter}}.
  *
  * ```php
  * Yii::$app->storage->addImage(123, 0); // create an image from file object id 123 without filter.
  * ```
  *
  * @param integer $fileId The id of the file where image should be created from.
  * @param integer $filterId The id of the filter which should be applied to, if filter is 0, no filter will be added. Filter can new also be the string name of the filter like `tiny-crop`.
  * @param boolean $throwException Whether the addImage should throw an exception or just return boolean
  * @return \luya\admin\image\Item|\luya\Exception|boolean Returns the item object, if an error happens and $throwException is off `false` is returned otherwhise an exception is thrown.
  */
 public function addImage($fileId, $filterId = 0, $throwException = false)
 {
     try {
         // if the filterId is provded as a string the filter will be looked up by its name in the get filters array list.
         if (is_string($filterId) && !is_numeric($filterId)) {
             $filterLookup = $this->getFiltersArrayItem($filterId);
             if (!$filterLookup) {
                 throw new Exception("The provided filter name " . $filterId . " does not exist.");
             }
             $filterId = $filterLookup['id'];
         }
         $query = (new \luya\admin\image\Query())->where(['file_id' => $fileId, 'filter_id' => $filterId])->one();
         if ($query && $query->fileExists) {
             return $query;
         }
         $fileQuery = $this->getFile($fileId);
         if (!$fileQuery || !$fileQuery->fileExists) {
             throw new Exception("Unable to create image, cause the base file does not exist.");
         }
         $fileName = $filterId . '_' . $fileQuery->systemFileName;
         $fileSavePath = $this->serverPath . '/' . $fileName;
         if (empty($filterId)) {
             $save = @copy($fileQuery->serverSource, $fileSavePath);
         } else {
             $model = StorageFilter::find()->where(['id' => $filterId])->one();
             if (!$model) {
                 throw new Exception("Could not find the provided filter id '{$filterId}'.");
             }
             if (!$model->applyFilterChain($fileQuery, $fileSavePath)) {
                 throw new Exception("Unable to create and save image '" . $fileSavePath . "'.");
             }
         }
         $resolution = Storage::getImageResolution($fileSavePath);
         // ensure the existing of the model
         $model = StorageImage::find()->where(['file_id' => $fileId, 'filter_id' => $filterId])->one();
         if ($model) {
             $model->updateAttributes(['resolution_width' => $resolution['width'], 'resolution_height' => $resolution['height']]);
         } else {
             $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("add image exception: " . $err->getMessage(), 0, $err);
         }
     }
     return false;
 }
Esempio n. 3
0
 /**
  * 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' => static::identifier()])->one();
     // if no model exists, create new record
     if (!$model) {
         $model = new StorageFilter();
         $model->setAttributes(['name' => $this->name(), 'identifier' => static::identifier()]);
         $model->insert(false);
         $this->addLog("added new filter '" . static::identifier() . "' to database.");
     }
     return $model;
 }