public function create($fileId, $filterId = 0) { // find image for file_id and filter_id $img = StorageImage::find()->where(['file_id' => $fileId, 'filter_id' => $filterId])->asArray()->one(); // the image exists already, we return the already created image id if ($img) { return $img['id']; } $file = Yii::$app->storage->file->getPath($fileId); if (!$file) { return false; } $info = Yii::$app->storage->file->getInfo($fileId); try { $imagine = new Imagine(); $image = $imagine->open($file); } catch (Exception $e) { return $e->getMessage(); } $fileName = $filterId . '_' . $info->name_new_compound; if (empty($filterId)) { $save = $image->save(Yii::$app->storage->dir . $fileName); } 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(Yii::$app->storage->dir . $fileName); } if ($save) { $model = new StorageImage(); $model->setAttributes(['file_id' => $fileId, 'filter_id' => $filterId]); if ($model->save()) { return $model->id; } } return false; }
/** * @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; }