Example #1
0
 /**
  * @param string $file Полный путь к исходному файлу в файловой системе
  * @param string $uploadDir Подпапка в папке с миниатюрами куда надо поместить изображение
  * @param float $width Ширина изображения. Если не указана - будет вычислена из высоты
  * @param float $height Высота изображения. Если не указана - будет вычислена из ширины
  * @param array $options
  * @return string
  * @throws CException
  */
 public function thumbnail($file, $uploadDir, $width = 0, $height = 0, array $options = ['jpeg_quality' => 90, 'png_compression_level' => 8])
 {
     if (!$width && !$height) {
         throw new CException("Incorrect width/height");
     }
     $name = $width . 'x' . $height . '_' . basename($file);
     $uploadPath = $this->getBasePath() . DIRECTORY_SEPARATOR . $uploadDir;
     $thumbFile = $uploadPath . DIRECTORY_SEPARATOR . $name;
     if (!file_exists($thumbFile)) {
         if (false === YFile::checkPath($uploadPath)) {
             throw new CException(Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', ['{dir}' => $uploadPath]));
         }
         $img = Imagine::getImagine()->open($file);
         $originalWidth = $img->getSize()->getWidth();
         $originalHeight = $img->getSize()->getHeight();
         if (!$width) {
             $width = $height / $originalHeight * $originalWidth;
         }
         if (!$height) {
             $height = $width / $originalWidth * $originalHeight;
         }
         if ($width / $originalWidth > $height / $originalHeight) {
             $box = new Box($width, $originalHeight * $width / $originalWidth);
         } else {
             $box = new Box($originalWidth * $height / $originalHeight, $height);
         }
         $img->resize($box)->crop(new Point(max(0, round(($box->getWidth() - $width) / 2)), max(0, round(($box->getHeight() - $height) / 2))), new Box($width, $height))->save($thumbFile, $options);
     }
     $url = $this->getBaseUrl() . '/' . $uploadDir . '/' . $name;
     return $url;
 }
Example #2
0
 /**
  * @param string $file Полный путь к исходному файлу в файловой системе
  * @param string $uploadDir Подпапка в папке с миниатюрами куда надо поместить изображение
  * @param float $width Ширина изображения. Если не указана - будет вычислена из высоты
  * @param float $height Высота изображения. Если не указана - будет вычислена из ширины
  * @param boolean $crop Обрезка миниатюры по размеру
  * @return string
  * @throws CException
  */
 public function thumbnail($file, $uploadDir, $width = 0, $height = 0, $crop = true)
 {
     if (!$width && !$height) {
         throw new CException("Incorrect width/height");
     }
     $name = !$crop ? $width . 'x' . $height . '_0_' . basename($file) : $width . 'x' . $height . '_' . basename($file);
     $uploadPath = $this->getBasePath() . DIRECTORY_SEPARATOR . $uploadDir;
     $thumbFile = $uploadPath . DIRECTORY_SEPARATOR . $name;
     $thumbMode = $crop ? ImageInterface::THUMBNAIL_OUTBOUND : ImageInterface::THUMBNAIL_INSET;
     if (!file_exists($thumbFile)) {
         if (false === YFile::checkPath($uploadPath)) {
             throw new CException(Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', ['{dir}' => $uploadPath]));
         }
         $img = Imagine::getImagine()->open($file);
         $originalWidth = $img->getSize()->getWidth();
         $originalHeight = $img->getSize()->getHeight();
         if (!$width) {
             $width = $height / $originalHeight * $originalWidth;
         }
         if (!$height) {
             $height = $width / $originalWidth * $originalHeight;
         }
         $img->thumbnail(new Box($width, $height), $thumbMode)->strip()->save($thumbFile, $this->options);
     }
     $url = $this->getBaseUrl() . '/' . $uploadDir . '/' . $name;
     return $url;
 }
Example #3
0
 /**
  * @throws \CException
  */
 public function saveFile()
 {
     if (!$this->resizeOnUpload) {
         return parent::saveFile();
     }
     $newFileName = $this->generateFilename();
     $path = $this->uploadManager->getFilePath($newFileName, $this->getUploadPath());
     if (!YFile::checkPath(pathinfo($path, PATHINFO_DIRNAME))) {
         throw new \CException(Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', ['{dir}' => $path]));
     }
     Imagine::resize($this->getUploadedFileInstance()->getTempName(), $this->resizeOptions['width'], $this->resizeOptions['height'])->save($path, $this->resizeOptions['quality']);
     $this->getOwner()->setAttribute($this->attributeName, $newFileName);
 }
 public function actionIndex()
 {
     exit;
     $imagesPath = Yii::app()->uploadManager->getBasePath() . DIRECTORY_SEPARATOR . 'realty' . DIRECTORY_SEPARATOR;
     $imagine = Imagine::getImagine();
     $transformation = new Transformation();
     $transformation->add(new Watermark($imagine, Yii::getPathOfAlias('webroot') . '/uploads/watersign.png'));
     if (file_exists($imagesPath)) {
         try {
             foreach (glob($imagesPath . '*.JPG') as $key => $path) {
                 if ($key > 200) {
                     break;
                 }
                 $transformation->apply($imagine->open($path))->save();
             }
             echo "success!!!";
         } catch (Exception $e) {
             echo 'Save operation failed: ' . $path . '<br />';
         }
     }
 }