protected function findModel($id) { if (($model = Slide::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionUploadSlide() { if (!isset($_POST['slide_id'], $_POST['attribute'], $_POST['slider_id'])) { throw new BadRequestHttpException(); } $model = Slider::findById($_POST['slider_id']); $slide = Slide::findOne($_POST['slide_id']); if ($model === null || $slide === null) { throw new NotFoundHttpException(); } $file = UploadedFile::getInstanceByName('file'); if ($file === null) { throw new HttpException(500, "Upload file error"); } if ($file->hasError) { throw new HttpException(500, 'Upload error'); } $fileName = $file->name; $uploadDir = Yii::getAlias(Yii::$app->getModule('core')->fileUploadPath); $fn = $uploadDir . $fileName; if (file_exists($fn)) { $fileName = $file->baseName . '-' . uniqid() . '.' . $file->extension; $fn = $uploadDir . $fileName; } $file->saveAs($fn); $image = \yii\imagine\Image::thumbnail($uploadDir . $fileName, $model->image_width, $model->image_height, ManipulatorInterface::THUMBNAIL_OUTBOUND); $image->save($uploadDir . 'small-' . $fileName, ['quality' => 95]); $slide->setAttribute($_POST['attribute'], str_replace(Yii::getAlias('@webroot'), '', $uploadDir) . 'small-' . $fileName); $slide->save(); return $this->redirect(['update', 'id' => $model->id]); }