Ejemplo n.º 1
0
 public function actionImage($id, $maxWidth, $thumbWidth, $thumbHeight = null, $thumbCrop = true)
 {
     $success = null;
     if ($photo = Photo::findOne($id)) {
         $oldImage = $photo->image;
         $oldThumb = $photo->thumb;
         $photo->image = UploadedFile::getInstance($photo, 'image');
         if ($photo->image && $photo->validate(['image'])) {
             $photo->image = Image::upload($photo->image, 'photos', $maxWidth);
             if ($photo->image) {
                 $photo->thumb = Image::createThumbnail($photo->image, $thumbWidth, $thumbHeight, $thumbCrop);
                 if ($photo->save()) {
                     @unlink(Yii::getAlias('@webroot') . $oldImage);
                     @unlink(Yii::getAlias('@webroot') . $oldThumb);
                     $success = ['message' => Yii::t('easyii', 'Photo uploaded'), 'photo' => ['thumb' => $photo->thumb, 'image' => $photo->image]];
                 } else {
                     @unlink(Yii::getAlias('@webroot') . $photo->image);
                     @unlink(Yii::getAlias('@webroot') . $photo->thumb);
                     $this->error = Yii::t('easyii', 'Update error. {0}', $photo->formatErrors());
                 }
             } else {
                 $this->error = Yii::t('easyii', 'File upload error. Check uploads folder for write permissions');
             }
         } else {
             $this->error = Yii::t('easyii', 'File is incorrect');
         }
     } else {
         $this->error = Yii::t('easyii', 'Not found');
     }
     return $this->formatResponse($success);
 }