src/storage/BaseFileSystemStorage.php
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) {
                if ($fileQuery !== false) {
                    throw new Exception("Unable to create image, the base file server source '{$fileQuery->serverSource}' does not exist.");
                }

                throw new Exception("Unable to find the file with id '{$fileId}', image can not be created.");
            }

            $model = $this->createImage($fileId, $filterId);

            if (!$model) {
                throw new Exception("Unable to create the image on the filesystem.");
            }

            $this->_imagesArray[$model->id] = $model->toArray();
            $this->deleteHasCache(self::CACHE_KEY_IMAGE);

            return $this->getImage($model->id);
        } catch (\Exception $err) {
            if ($throwException) {
                throw $err;
            }
        }

        return false;
    }
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) {
                if ($fileQuery !== false) {
                    throw new Exception("Unable to create image, the base file server source '{$fileQuery->serverSource}' does not exist.");
                }

                throw new Exception("Unable to find the file with id '{$fileId}', image can not be created.");
            }

            $model = $this->createImage($fileId, $filterId);

            if (!$model) {
                throw new Exception("Unable to create the image on the filesystem.");
            }

            $this->_imagesArray[$model->id] = $model->toArray();
            $this->deleteHasCache(self::CACHE_KEY_IMAGE);

            return $this->getImage($model->id);
        } catch (\Exception $err) {
            if ($throwException) {
                throw $err;
            }
        }

        return false;
    }
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 ) { if ( $ fileQuery !== false ) { throw new Exception ( "Unable to create image, the base file server source '{$fileQuery->serverSource}' does not exist." ) ; } throw new Exception ( "Unable to find the file with id '{$fileId}', image can not be created." ) ; } $ model = $ this -> createImage ( $ fileId , $ filterId ) ; if ( ! $ model ) { throw new Exception ( "Unable to create the image on the filesystem." ) ; } $ this -> _imagesArray [ $ model -> id ] = $ model -> toArray ( ) ; $ this -> deleteHasCache ( self :: CACHE_KEY_IMAGE ) ; return $ this -> getImage ( $ model -> id ) ; } catch ( \ Exception $ err ) { if ( $ throwException ) { throw $ err ; } } return false ; }
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 bool|\luya\admin\image\Item|Exception Returns the item object, if an error happens and $throwException is off `false` is returned otherwhise an exception is thrown.
@throws \luya\Exception
Add a new image based an existing file Id .