public function __toString() { if ($this->adaptive) { $this->gd->adaptiveResize($this->width, $this->height); } else { $this->gd->resize($this->width, $this->height); } return sprintf('data:%s;base64,%s', $this->gd->getFormat(), base64_encode($this->gd->getImageAsString())); }
/** * Get resized image * @param ImageParams $params * @return MongoGridFSFile */ public function get(ImageParams $params = null) { // Get original image or file if it is not image if (!$params || !$this->isImage($this->filename)) { return $this->_get(); } // Get resized image $params->isTemp = true; $result = $this->_get($params->toArray()); // Resize and store if not found if (!$result) { $result = $this->_get(); if (!$result) { throw new Exception('File not found'); } $originalFilename = $result->file['filename']; $fileName = tempnam('/tmp/', __CLASS__); $result->write($fileName); $image = new GD($fileName); if ($params->adaptive) { $image->adaptiveResize($params->width, $params->height)->save($fileName); } else { $image->resize($params->width, $params->height)->save($fileName); } $this->_set($fileName, $originalFilename, $params->toArray()); unlink($fileName); return $this->_get($params->toArray()); } return $result; }
/** * 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 = 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; }