Maslosoft Staple API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/staple
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @link http://maslosoft.com/staple/
*/
namespace Maslosoft\Staple\Renderers;
use Maslosoft\Staple\Exceptions\NotFoundException;
use Maslosoft\Staple\Interfaces\RendererExtensionInterface;
use Maslosoft\Staple\Interfaces\RendererInterface;
use Maslosoft\Staple\Interfaces\VirtualInterface;
use PHPThumb\GD;
use SplFileInfo;
/**
* ThumbRenderer
* TODO: Create image thumbs out of an image.
* 1. Should use extensions like: thumb.jpg, thumb.png
* 3. Should create a thumb with predefined or requested size and cache it in browser
* 4. Should be implemented somewhat similar to PassThroughRenderer
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class ThumbRenderer extends AbstractRenderer implements RendererInterface, RendererExtensionInterface, VirtualInterface
{
const OptionCrop = 'c';
private $extension = '';
public $width = 300;
public $height = 200;
public $options = [];
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();
}
public function setExtension($extension)
{
$this->extension = $extension;
}
}
API documentation generated by ApiGen