/** * @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; }
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(); }
/** * @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(); }
/** * @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; }
/** * @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); }
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; }
/** * @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 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; }
/** * @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; }
/** * @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); }