Exemplo n.º 1
0
 public function actionImageRotate()
 {
     $result = [];
     $this->layout = 'json';
     $ref = Yii::$app->request->get('ref');
     $id = Yii::$app->request->get('id');
     $photo_id = Yii::$app->request->get('photo_id', '');
     $deg = intval(Yii::$app->request->get('deg', 0));
     $page = Yii::$app->request->get('page', -1);
     $place_num = intval(Yii::$app->request->get('place_num', -1));
     if (!empty($id)) {
         $model = new PhotobookForm();
         $user_id = AlphaId::id($ref, true);
         $pb_id = AlphaId::id($id, true);
         if ($model->loadById($pb_id)) {
             $file_path = UserUrl::photobookPhotos(false, $pb_id, $user_id);
             $paths = [];
             $paths[] = $file_path;
             $ext = 'jpg';
             if ($model->isText($photo_id)) {
                 $ext = 'png';
             }
             $photo_o_path = $file_path . DIRECTORY_SEPARATOR . UserUrl::imageFile($photo_id, UserUrl::IMAGE_ORIGINAL, $ext);
             $image = Yii::$app->image->load($photo_o_path);
             $image->rotate($deg);
             $image->save($photo_o_path);
             foreach (UserUrl::$IMAGE_SIZE as $key => $size) {
                 if ($key != UserUrl::IMAGE_ORIGINAL) {
                     $image = Yii::$app->image->load($photo_o_path);
                     $type = Yii\image\drivers\Image::HEIGHT;
                     if ($size['width'] > 0 && $size['height'] > 0) {
                         $type = Yii\image\drivers\Image::AUTO;
                     }
                     if ($size['width'] > 0 && $size['height'] == 0) {
                         $type = Yii\image\drivers\Image::WIDTH;
                     }
                     $file_resize_path = UserUrl::photobookPhotos(false, $pb_id, $user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($photo_id, $key, $ext);
                     $image->resize($size['width'], $size['height'], $type);
                     $image->save($file_resize_path);
                     unset($image);
                     $image = null;
                 }
             }
             if ($page < 0 || $place_num < 0) {
                 $result = ['response' => ['status' => true, 'photo_id' => $photo_id]];
             } else {
                 $result = $model->setImagePosAndScale($page, $place_num, 0, 0, 1);
             }
             $last_modified = filemtime($photo_o_path);
             if (!empty($result['response'])) {
                 $result['response']['photo_id'] = $photo_id;
                 $result['response']['page_index'] = $page;
                 $result['response']['place_num'] = $place_num;
                 $result['response']['last_modified'] = $last_modified;
             }
         } else {
             $result = ['error' => ['msg' => Yii::t('app', 'Фотокнига не найдена')]];
         }
     } else {
         $result = ['error' => ['msg' => Yii::t('app', 'Фотокнига не найдена')]];
     }
     return $this->render('json', ['result' => $result]);
 }