public function actionImage($id) { $success = null; if ($photo = Photo::findOne($id)) { $oldImage = $photo->image; $photo->image = UploadedFile::getInstance($photo, 'image'); if ($photo->image && $photo->validate(['image'])) { $photo->image = Image::upload($photo->image, 'photos', Photo::PHOTO_MAX_WIDTH); if ($photo->image) { if ($photo->save()) { @unlink(Yii::getAlias('@webroot') . $oldImage); $success = ['message' => Yii::t('easyii', 'Photo uploaded'), 'photo' => ['image' => $photo->image, 'thumb' => Image::thumb($photo->image, Photo::PHOTO_THUMB_WIDTH, Photo::PHOTO_THUMB_HEIGHT)]]; } else { @unlink(Yii::getAlias('@webroot') . $photo->image); $this->error = Yii::t('easyii', 'Update error. {0}', $photo->formatErrors()); } } else { $this->error = Yii::t('easyii', 'File upload error. Check uploads folder for write permissions'); } } else { $this->error = Yii::t('easyii', 'File is incorrect'); } } else { $this->error = Yii::t('easyii', 'Not found'); } return $this->formatResponse($success); }
public function actionUpload($dir = '') { $fileInstance = UploadedFile::getInstanceByName('file'); if ($fileInstance) { $file = Image::upload($fileInstance, $dir); if ($file) { return $this->getResponse($file); } } return ['error' => 'Unable to save image file']; }
public function actionEdit($id) { if (!($model = Item::findOne($id))) { return $this->redirect(['/populac/' . $this->module->id]); } if ($model->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } else { if (isset($_FILES) && $this->module->settings['articleThumb']) { $model->image = UploadedFile::getInstance($model, 'image'); if ($model->image && $model->validate(['image'])) { $model->image = Image::upload($model->image, 'article'); } else { $model->image = $model->oldAttributes['image']; } } if ($model->save()) { $this->flash('success', Yii::t('easyii/article', 'Article updated')); return $this->redirect(['/populac/' . $this->module->id . '/items/edit', 'id' => $model->primaryKey]); } else { $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors())); return $this->refresh(); } } } else { return $this->render('edit', ['model' => $model]); } }
/** * Edit form * * @param $id * @return array|string|\yii\web\Response * @throws \yii\web\HttpException */ public function actionEdit($id) { $class = $this->categoryClass; if (!($model = $class::findOne($id))) { return $this->redirect(['/populac/' . $this->moduleName]); } if ($model->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } else { if (isset($_FILES) && $this->module->settings['categoryThumb']) { $model->image = UploadedFile::getInstance($model, 'image'); if ($model->image && $model->validate(['image'])) { $model->image = Image::upload($model->image, $this->moduleName); } else { $model->image = $model->oldAttributes['image']; } } if ($model->save()) { $this->flash('success', Yii::t('easyii', 'Category updated')); } else { $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors())); } return $this->refresh(); } } else { return $this->render('@common/populac/views/category/edit', ['model' => $model]); } }