예제 #1
0
 /**
  * Creates a new News model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new News();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
예제 #2
0
 /**
  * Creates a new News model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new News();
     if ($model->load(Yii::$app->request->post())) {
         $model->save();
         $last = $model->getPrimaryKey();
         $file = UploadedFile::getInstance($model, 'file');
         if ($file && $file->tempName) {
             $model->file = $file;
             if ($model->validate(['file'])) {
                 $dir = Yii::getAlias('uploads/News/');
                 $fileName = 'news_image_' . $last . '.jpeg';
                 $model->file->saveAs($dir . $fileName);
                 $model->file = $fileName;
                 // без этого ошибка
                 Image::thumbnail($dir . $fileName, 240, 180)->save(Yii::getAlias($dir . 'thumbs/' . $fileName), ['quality' => 80]);
             }
         }
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }