/** * Creates a new Photo model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Photo(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('photoCreate', ['model' => $model]); } }
/** * 保存批量提交申请 * @param $album_id */ public function actionSaveBatchCreate() { try { $connection = \Yii::$app->db; $transaction = $connection->beginTransaction(); $post = Yii::$app->request->post(); $album_id = $post['Photo']['album_id']; $is_album_cover = isset($post['Photo']['is_album_cover']) ? $post['Photo']['is_album_cover'] : ''; if (!empty($post['Photo']['img_url'])) { foreach ($post['Photo']['img_url'] as $key => $img_url) { if (empty($img_url)) { continue; } //判断传值的图片文件是否存在 $img_path = $_SERVER['DOCUMENT_ROOT'] . $img_url; $full_path = $this->getFullPath(); $absolute_path = $_SERVER['DOCUMENT_ROOT'] . $full_path; if (!file_exists($img_path)) { throw new \Exception('图片文件不存在'); } if (!file_exists($absolute_path) && !mkdir($absolute_path, 0777, true)) { throw new \Exception('ERROR_CREATE_DIR'); } else { if (!is_writeable($absolute_path)) { throw new \Exception('ERROR_DIR_NOT_WRITEABLE'); } } $imageSize = Image::getImagine()->open($img_path)->getSize(); $imageFormat = $this->getFullSize($imageSize->getWidth(), $imageSize->getHeight()); $Photo = new Photo(); $Photo->album_id = $album_id; $Photo->desc = $post['Photo']['desc'][$key]; $Photo->title = $post['Photo']['title'][$key]; if ($is_album_cover == $img_url) { Photo::updateAll(['is_album_cover' => '0'], 'album_id = ' . $album_id); $Photo->is_album_cover = '1'; } if (!empty($imageFormat)) { foreach ($imageFormat as $key => $val) { $full_name = $this->getFullName($this->getFileName($img_path)); //移动图片到指定目录 //生成缩略文件 Image::thumbnail($img_path, $val[0], $val[1])->save($absolute_path . $full_name); $Photo->{$key} = $full_path . $full_name; } } $Photo->save(); } } Photo::updatePrevNextAndCover($album_id); $transaction->commit(); } catch (\Exception $exp) { $transaction->rollBack(); $message = ''; $result = ['statusCode' => '300', 'message' => $exp->getMessage(), 'navTabId' => 'photo_photo_batch_create_id', 'forwardUrl' => '', 'callbackType' => '']; echo Json::encode($result); exit; } $result = ['statusCode' => '200', 'message' => '操作成功', 'navTabId' => 'photo_photo_index_id', 'forwardUrl' => '', 'callbackType' => 'closeCurrent']; echo Json::encode($result); exit; }