public function getThumb($path, $variant = 'default') { $var = $this->variants[$variant]; if (!$var) { throw new \Exception('Incorrect thumb type: ' . $variant); } $filePath = $this->getThumbPath($path, $variant); if (file_exists($filePath) && is_file($filePath)) { return realpath($filePath); } try { $thumb = new GD($path); } catch (\Exception $e) { return false; } $operation = $var['operation'] ?: 'resize'; $thumb->{$operation}($var['maxWidth'], $var['maxHeight']); $thumb->setOptions(['jpegQuality' => $var['quality']]); $dir = dirname($filePath); if (!file_exists($dir) || !is_dir($dir)) { mkdir($dir, 0755, true); } $thumb->save($filePath, $var['format']); return realpath($filePath); }
/** * Creates image thumbnails */ public function createThumbs() { $path = $this->getUploadedFilePath($this->attribute); foreach ($this->thumbs as $profile => $config) { $thumbPath = static::getThumbFilePath($this->attribute, $profile); if (!is_file($thumbPath)) { /** @var GD $thumb */ $thumb = new GD($path); $thumb->adaptiveResize($config['width'], $config['height']); FileHelper::createDirectory(pathinfo($thumbPath, PATHINFO_DIRNAME), 0775, true); $thumb->save($thumbPath); } } }
public function createThumbs() { $path = $this->getUploadedFilePath($this->attribute); if (file_exists($path)) { foreach ($this->thumbs as $profile => $config) { $thumbPath = static::getThumbFilePath($this->attribute, $profile); if (!is_file($thumbPath)) { /** @var GD $thumb */ $thumb = new GD($path); $dimensions = $thumb->getCurrentDimensions(); if ($dimensions['width'] > $dimensions['height']) { $thumb->resize($config['width'], $config['height']); } else { $thumb->resize($config['height'], $config['width']); } FileHelper::createDirectory(pathinfo($thumbPath, PATHINFO_DIRNAME), 0775, true); $thumb->save($thumbPath); if (isset($config['watermark'])) { $watermarkedImage = Image::watermark($thumbPath, $config['watermark']); $watermarkedImage->save($thumbPath); } } } } }
public function createThumbs() { $path = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $this->resolvePath(); foreach ($this->thumbs as $profile => $config) { $thumbPath = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $this->resolveThumbPath($profile); if (!is_file($thumbPath)) { /** @var GD $thumb */ $thumb = new GD($path); if (isset($config['resizeUp'])) { $thumb->setOptions(array('resizeUp' => $config['resizeUp'])); } if (isset($config['adaptive']) && $config['adaptive'] === false) { $thumb->resize($config['width'], $config['height']); } else { //By default we do adaptiveResize $thumb->adaptiveResize($config['width'], $config['height']); } @mkdir(pathinfo($thumbPath, PATHINFO_DIRNAME), 777, true); $thumb->save($thumbPath); } } }
public function render($view = 'index', $data = array()) { $view = urldecode($view); $contentPath = $this->getOwner()->getContentPath(); $rootPath = $this->getOwner()->getRootPath(); $matches = []; if (preg_match('~@\\((\\d+)x(\\d+)\\)([a-z]{1})?$~', $view, $matches)) { $this->width = $matches[1]; $this->height = $matches[2]; if (isset($matches[3])) { $this->options = preg_split('~~', $matches[3], PREG_SPLIT_NO_EMPTY); } $pattern = preg_quote($matches[0]); $baseView = preg_replace("~{$pattern}\$~", '', $view); } $thumbName = sprintf('%s/%s.%s', $rootPath, $view, $this->extension); // Get thumbnail dir for later use $thumbDir = dirname($thumbName); // Try to make a thumbnail dir if (!file_exists($thumbDir)) { @mkdir($thumbDir, 0777, true); } $baseExt = str_replace('thumb.', '', $this->extension); $fileName = sprintf('%s/%s.%s', $contentPath, $baseView, $baseExt); if (!file_exists($fileName)) { throw new NotFoundException(sprintf('File not found: `%s`', $fileName)); } if (!file_exists($thumbName)) { $image = new GD($fileName); if (in_array(self::OptionCrop, $this->options)) { $image->adaptiveResize($this->width, $this->height); } else { $image->resize($this->width, $this->height); } // ensure we can write into dir or overwrite a file if (is_writeable($thumbDir) || is_writeable($thumbName)) { $image->save($thumbName); } } $info = new SplFileInfo($thumbName); $size = $info->getSize(); if ($size > 0) { header(sprintf('Content-Length: %d', $size)); } switch (strtolower($info->getExtension())) { case 'gif': header('Content-type: image/gif'); break; case 'jpg': header('Content-type: image/jpeg'); break; case 'png': case 'string': header('Content-type: image/png'); break; } header(sprintf('ETag: %s', md5($thumbName))); header(sprintf('Last-Modified: %s', gmdate('D, d M Y H:i:s \\G\\M\\T', $info->getMTime()))); header(sprintf('Content-Disposition: filename="%s"', basename($fileName))); // Cache it header('Pragma: public'); header('Cache-Control: max-age=86400'); header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 86400)); echo file_get_contents($thumbName); exit; }