Esempio n. 1
0
 public function run()
 {
     // folder for uploaded files
     $tempFolder = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . $this->tempFolder;
     if (!is_writable($tempFolder)) {
         throw new CException('temporary folder is not exists or not writable. Path:' . $tempFolder);
     }
     $uploader = new FileUploader($this->allowedFileExtensions, $this->fileLimit);
     $result = $uploader->handleUpload($tempFolder);
     if (!isset($result['error'])) {
         $imageHandler = new CImageHandler();
         $imageHandler->load($tempFolder . $result['filename']);
         try {
             // if min/max weight/height are set - check those conditions
             $this->validateImageDimensions($imageHandler->getWidth(), $imageHandler->getHeight());
             $this->getController()->successfulAjaxResponse(array('fileName' => $imageHandler->getBaseFileName(), 'imageSrc' => '/application' . $this->tempFolder . $imageHandler->getBaseFileName()));
         } catch (CException $e) {
             $errorMsg = $imageHandler->getBaseFileName() . ' - ' . $e->getMessage();
             $this->getController()->unsuccessfulAjaxResponse(array('errorMessage' => $errorMsg));
         }
     } else {
         $this->getController()->unsuccessfulAjaxResponse(array('errorMessage' => $result['error']));
     }
 }
 /**
  * Copy image from temporary folder to proper destination and create thumbnails
  */
 protected function saveImage()
 {
     $imageHandler = new CImageHandler();
     $imageHandler->load($this->getTempFolder(TRUE) . $this->image);
     $imageHandler->save($this->getImagesFolder(TRUE) . $imageHandler->getBaseFileName());
     $settings = $this->getThumbsSettings();
     if (!empty($settings)) {
         $imageHandler = new CImageHandler();
         $imageHandler->load($this->getTempFolder(TRUE) . $this->image);
         foreach ($settings as $prefix => $dimensions) {
             list($width, $height) = $dimensions;
             $imageHandler->thumb($width, $height)->save($this->getImagesFolder(TRUE) . $prefix . $imageHandler->getBaseFileName());
         }
     }
 }