예제 #1
0
 /**
  * Action for uploading an image using AJAX.
  * @param string $name name for locating the uploaded file.
  * @param string $preset name of the preset.
  * @param string $saveName image name.
  * @param string $path image path.
  * @throws CException if the uploaded file is not found.
  */
 public function actionAjaxUpload($name, $preset = null, $saveName = null, $path = null)
 {
     $ajax = new AjaxResponse();
     $file = CUploadedFile::getInstanceByName($name);
     if ($file === null) {
         $ajax->error(sprintf('Uploaded file with name "%s" could not be found.', $name));
     }
     $manager = $this->getManager();
     try {
         $model = $manager->saveModel(new UploadedFile($file), $saveName, $path);
         $ajax->add('imageId', $model->id);
         if ($preset !== null) {
             $preset = $manager->loadPreset($preset);
             $ajax->add('imageTooSmall', $preset->getWidth() > $model->width || $preset->getHeight() > $model->height);
             $ajax->add('imageUrl', $manager->createImagePresetUrl($model, $preset));
         }
         $ajax->success();
     } catch (Exception $e) {
         Yii::log(sprintf('Image upload failed with error: %s', $e->getMessage()), CLogger::LEVEL_ERROR, 'ImageManager');
         $ajax->error(t('imageManager', 'Something went wrong when uploading the image, please try again.'));
     }
 }