Inheritance: extends CFileHelpe\CFileHelper
 public function saveFile()
 {
     if (!$this->resizeOnUpload) {
         parent::saveFile();
         return;
     }
     $newFileName = $this->getFileName();
     $path = Yii::app()->uploadManager->getFilePath($newFileName, $this->getUploadPath());
     if (!YFile::checkPath(pathinfo($path, PATHINFO_DIRNAME))) {
         throw new \CHttpException(500, Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', array('{dir}' => $path)));
     }
     /*
             $image = Imagine::resize(
        $this->_currentFile->getTempName(),
        $this->resizeOptions['width'],
        $this->resizeOptions['height']
             );
     
             $image->save(
        $path,
        $this->resizeOptions['quality']
             );
     * 
     */
     $image = Yii::app()->image->load($this->_currentFile->getTempName())->quality($this->resizeOptions['quality']);
     $image->resize($this->resizeOptions['width'], $this->resizeOptions['height'], $this->resizeOptions['master']);
     $image->save($path);
     $this->getOwner()->{$this->attributeName} = $newFileName;
     $this->_prevFile = $this->getPrevFile();
 }
Example #2
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;
 }
 /**
  * @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]));
     }
     if (!$this->resizeOnUpload) {
         $img = Imagine::getImagine()->open($this->getUploadedFileInstance()->getTempName())->strip()->save($path);
     } else {
         Imagine::resize($this->getUploadedFileInstance()->getTempName(), $this->resizeOptions['width'], $this->resizeOptions['height'])->save($path, $this->resizeOptions['quality']);
     }
     // Добавляем watermark
     if ($this->setWatermarkOnUpload) {
         Imagine::addWatermark($path, Yii::getPathOfAlias('webroot') . $this->watermark);
     }
     $this->getOwner()->setAttribute($this->attributeName, $newFileName);
     $this->_prevFile = $this->getPrevFile();
 }
Example #4
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 #5
0
 /**
  * @param  CUploadedFile $fileInstance
  * @param  string $uploadPath - file path
  * @param  string $fileName - file name
  * @return bool
  *
  * Сохранение загруженного файла
  */
 public function save(CUploadedFile $fileInstance, $uploadPath, $fileName)
 {
     $path = $this->getFilePath($fileName, $uploadPath);
     if (!YFile::checkPath(pathinfo($path, PATHINFO_DIRNAME))) {
         throw new \CHttpException(500, Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', ['{dir}' => $path]));
     }
     return $fileInstance->saveAs($path);
 }
Example #6
0
 public function checkSelf()
 {
     $messages = [];
     if (!YFile::checkPath($this->updateTmpPath)) {
         $messages[WebModule::CHECK_ERROR][] = ['type' => WebModule::CHECK_ERROR, 'message' => Yii::t('UpdateModule.update', 'Please, choose catalog for updates!')];
     }
     if (!YFile::checkPath(Yii::getPathOfAlias("application.modules"))) {
         $messages[WebModule::CHECK_ERROR][] = ['type' => WebModule::CHECK_ERROR, 'message' => Yii::t('UpdateModule.update', 'Directory {dir} is not writable!', ['{dir}' => Yii::getPathOfAlias("application.modules")])];
     }
     return isset($messages[WebModule::CHECK_ERROR]) ? $messages : true;
 }
 public function actionRegenerate()
 {
     if (!Yii::app()->getRequest()->getIsPostRequest() || !Yii::app()->getRequest()->getPost('do')) {
         throw new CHttpException(404);
     }
     if (\yupe\helpers\YFile::rmIfExists($this->getModule()->getSiteMapPath())) {
         Yii::app()->getUser()->setFlash(YFlashMessages::SUCCESS_MESSAGE, Yii::t('SitemapModule.sitemap', 'Sitemap is deleted!'));
         Yii::app()->ajax->success();
     }
     Yii::app()->getUser()->setFlash(YFlashMessages::ERROR_MESSAGE, Yii::t('SitemapModule.sitemap', 'Sitemap is not deleted!'));
     Yii::app()->ajax->failure();
 }
Example #8
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);
 }
Example #9
0
 public function makeThumbnail($file, $uploadDir, $width, $height, $mode = ImageInterface::THUMBNAIL_OUTBOUND, $saveOptions = array())
 {
     $name = $width . 'x' . $height . '_' . $mode . '_' . $file;
     $cacheId = $this->cachePrefix . $name;
     $url = Yii::app()->cache->get($cacheId);
     if (false === $url) {
         try {
             $path = $this->getUploadManager()->getBasePath() . DIRECTORY_SEPARATOR . $uploadDir . DIRECTORY_SEPARATOR . $this->thumbDir;
             if (false === YFile::checkPath($path)) {
                 throw new CHttpException(500, Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', array('{dir}' => $path)));
             }
             if (!file_exists($path . DIRECTORY_SEPARATOR . $name)) {
                 Imagine::thumbnail($this->getUploadManager()->getFilePath($file, $uploadDir), $width, $height, $mode)->save($path . DIRECTORY_SEPARATOR . $name, $saveOptions);
             }
             $url = $this->getUploadManager()->getBaseUrl() . '/' . $uploadDir . '/' . $this->thumbDir . '/' . $name;
             Yii::app()->cache->set($cacheId, $url);
         } catch (\Exception $e) {
             Yii::log($e->__toString(), \CLogger::LEVEL_ERROR);
             return null;
         }
     }
     return $url;
 }
Example #10
0
 /**
  * @param $file string Полный путь к исходному файлу в файловой системе
  * @param $uploadDir string Подпапка в папке с миниатюрами куда надо поместить изображение
  * @param $width
  * @param $height
  * @param string $mode
  * @param array $options
  * @return mixed|string
  * @throws CHttpException
  */
 public function thumbnail($file, $uploadDir, $width, $height, $mode = self::ADAPTIVE, $options = array())
 {
     $name = $width . 'x' . $height . '_' . $mode . '_' . basename($file);
     $cacheId = $this->cachePrefix . '::' . $file . '::' . $name;
     $url = Yii::app()->cache->get($cacheId);
     if (false === $url) {
         $uploadPath = $this->getBasePath() . DIRECTORY_SEPARATOR . $uploadDir;
         if (false === YFile::checkPath($uploadPath)) {
             throw new CHttpException(500, Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', array('{dir}' => $uploadPath)));
         }
         $thumbFile = $uploadPath . DIRECTORY_SEPARATOR . $name;
         if (!file_exists($thumbFile)) {
             /*
                             Imagine::thumbnail(
                $file,
                $width,
                $height,
                $mode
                             )->save($thumbFile, $options);
             * 
             */
             $thumb = Yii::app()->thumbs->create($file);
             if ($mode == self::MODE_QUADRANT) {
                 $thumb->adaptiveResizeQuadrant($width, $height, $options['quadrant']);
             } elseif ($mode == self::MODE_ADAPTIVE) {
                 $thumb->adaptiveResize($width, $height);
             } else {
                 $thumb->resize($width, $height);
             }
             $thumb->save($thumbFile);
         }
         $url = $this->getBaseUrl() . '/' . $uploadDir . '/' . $name;
         //Yii::app()->cache->set($cacheId, $url);
     }
     return $url;
 }
Example #11
0
 public function saveImage()
 {
     $quality = isset($this->resize['quality']) ? $this->resize['quality'] : 100;
     $width = isset($this->resize['width']) ? $this->resize['width'] : null;
     $height = isset($this->resize['height']) ? $this->resize['height'] : null;
     $tmpName = $this->_newImage->tempName;
     $imageName = $this->_getImageName();
     $image = Yii::app()->image->load($tmpName)->quality($quality);
     if (!($newFile = YFile::pathIsWritable($imageName, $image->ext, $this->uploadPath))) {
         throw new CHttpException(500, Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', array('{dir}' => $this->uploadPath)));
     }
     if ($width !== null && $image->width > $width || $height !== null && $image->height > $height) {
         $image->resize($width, $height);
     }
     if ($image->save($newFile)) {
         $this->owner->{$this->attributeName} = pathinfo($newFile, PATHINFO_BASENAME);
     }
 }
Example #12
0
 /**
  * @param CUploadedFile $fileInstance
  * @param string $uploadPath - file path
  * @param string $fileName - file name
  * @return bool
  *
  * Сохранение загруженного файла
  */
 public function save(CUploadedFile $fileInstance, $uploadPath, $fileName)
 {
     $path = $this->getFilePath($fileName, $uploadPath);
     YFile::checkPath(pathinfo($path, PATHINFO_DIRNAME));
     return $fileInstance->saveAs($path);
 }
Example #13
0
 /**
  * @return null|string
  */
 public function getFilePath()
 {
     if (!$this->attribute->isType(Attribute::TYPE_FILE)) {
         return null;
     }
     $file = Yii::app()->getBasePath() . '/' . Yii::app()->getModule('yupe')->uploadPath . '/' . Yii::app()->getModule('store')->uploadPath . '/product/' . $this->value();
     return \yupe\helpers\YFile::rmFile($file);
 }
Example #14
0
 /**
  * Команда для очистки папки assets.
  *
  * Examples:
  *
  * yiic yupe flushAssets
  *
  * @return bool
  */
 public function actionFlushAssets()
 {
     $dirs = glob(Yii::getPathOfAlias('webroot.assets') . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
     foreach ($dirs as $value) {
         if (!\yupe\helpers\YFile::rmDir($value)) {
             $this->log('Failed to remove directory "' . $value . '"', CLogger::LEVEL_ERROR);
         }
     }
     return true;
 }
Example #15
0
 /**
  * Обновить конфигурационный файл модуля
  *
  * @param WebModule $module
  * @return bool
  * @since 0.8
  */
 public function updateModuleConfig(WebModule $module)
 {
     $newConfig = $this->getModulesConfigDefault($module->getId());
     $currentConfig = $this->getModulesConfig($module->getId());
     if ((!file_exists($currentConfig) || YFile::rmFile($currentConfig)) && YFile::cpFile($newConfig, $currentConfig)) {
         return true;
     }
     return false;
 }
Example #16
0
 protected function cleanUp($module, $version, $rmUploaded = false)
 {
     Yii::log(sprintf('Start cleanup module "%s" version "%s"...', $module, $version), \CLogger::LEVEL_INFO, static::LOG_CATEGORY);
     $destination = Yii::getPathOfAlias('application.modules') . DIRECTORY_SEPARATOR . $module . '-' . $this->escapeVersion($version);
     if (is_dir($destination)) {
         Yii::log(sprintf('Deleting %s dir...', $destination), \CLogger::LEVEL_INFO, static::LOG_CATEGORY);
         YFile::rmDir($destination);
     }
     $modulePath = Yii::getPathOfAlias("application.modules.{$module}");
     $backupPath = $modulePath . '_';
     if (is_dir($backupPath)) {
         Yii::log(sprintf('Deleting backup path %s...', $backupPath), \CLogger::LEVEL_INFO, static::LOG_CATEGORY);
         YFile::rmDir($backupPath);
     }
     if ($rmUploaded) {
         YFile::rmIfExists($this->getUploadPathForModule($module, $this->escapeVersion($version)));
         YFile::rmIfExists($this->getUploadPathForModule($module, $this->escapeVersion($version), true));
     }
     Yii::log(sprintf('Stop cleanup module "%s" version "%s"...', $module, $version), \CLogger::LEVEL_INFO, static::LOG_CATEGORY);
 }