示例#1
0
 protected function findModel($id)
 {
     if (($model = YiiUser::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * @裁剪头像
  */
 public function actionCutpic()
 {
     if (Yii::$app->request->isAjax) {
         $path = "/avatar/";
         $targ_w = $targ_h = 150;
         $jpeg_quality = 100;
         $src = Yii::$app->request->post('f');
         $src = Yii::$app->basePath . '/web' . $src;
         //真实的图片路径
         $img_r = imagecreatefromjpeg($src);
         $ext = $path . time() . ".jpg";
         //生成的引用路径
         $dst_r = ImageCreateTrueColor($targ_w, $targ_h);
         imagecopyresampled($dst_r, $img_r, 0, 0, Yii::$app->request->post('x'), Yii::$app->request->post('y'), $targ_w, $targ_h, Yii::$app->request->post('w'), Yii::$app->request->post('h'));
         $img = Yii::$app->basePath . '/web/' . $ext;
         //真实的图片路径
         if (imagejpeg($dst_r, $img, $jpeg_quality)) {
             //更新用户头像
             $user = YiiUser::findOne(Yii::$app->user->getId());
             $user->thumb = $ext;
             $user->save();
             $arr['status'] = 1;
             $arr['data'] = $ext;
             $arr['info'] = '裁剪成功!';
             echo json_encode($arr);
         } else {
             $arr['status'] = 0;
             echo json_encode($arr);
         }
         exit;
     }
 }