hedera-web/rest/image/thumb.php

98 lines
2.2 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
2016-08-25 10:47:09 +00:00
require_once (__DIR__.'/util.php');
2016-07-22 20:00:27 +00:00
/**
* Creates a thumb from an existing full image.
*
* @param string $schema The name of schema
* @param string $file The file name
* @param integer $width The width of the thumb
* @param integer $height The height of the thumb
2016-12-20 09:32:17 +00:00
*/
2016-08-25 10:47:09 +00:00
class Thumb extends Vn\Web\RestRequest
2016-07-22 20:00:27 +00:00
{
function run ()
{
// XXX: Uncomment only to test the script
//$_SERVER['REQUEST_URI'] = 'catalog/200x200/e_cinerea.png';
$db = $this->getSysConn ();
// Gets parameters from URI.
$uriSplit = explode ('/', $_SERVER['REQUEST_URI']);
$uriSplit = array_slice ($uriSplit, count ($uriSplit) - 3, 3);
if (count ($uriSplit) < 3)
throw new Exception ('Bad request');
$schema = $uriSplit[0];
$orgFile = $uriSplit[2];
$file = $orgFile;
if (strrpos ($file, '.') === FALSE)
$file .= '.png';
$size = explode ('x', $uriSplit[1]);
if (count ($size) < 2)
throw new Exception ('Bad request');
$width = (int) $size[0];
$height = (int) $size[1];
// Verifies that it is an allowed size.
$params = [
'schema' => $schema
,'width' => $width
,'height' => $height
];
$row = $db->getValue (
'SELECT crop
FROM image_schema s
JOIN image_schema_size z ON z.image_schema_id = s.id
WHERE s.name = #schema
AND z.width = #width
AND z.height = #height'
,$params
);
if (!isset ($row))
throw new Exception ('Size not allowed');
// Creates the thumb.
2016-08-25 10:47:09 +00:00
$util = new Util ($this->app);
$baseDir = "{$util->dataDir}/$schema";
2016-07-22 20:00:27 +00:00
$srcFile = "$baseDir/full/$file";
$dstFile = "$baseDir/{$width}x{$height}/$file";
$symbolicSrc = "../full/$file";
if (!file_exists ($srcFile))
throw new Exception ('Source not exists');
if (file_exists ($dstFile))
throw new Exception ('Destination already exists');
$image = Image::create ($srcFile);
Image::resizeSave ($image, $dstFile, $height, $width, $row, $symbolicSrc);
imagedestroy ($image);
// Sends the thumb to the client
$useXsendfile = $db->getValue ('SELECT use_xsendfile FROM image_config');
if ($useXsendfile)
{
header ("X-Sendfile: $dstFile");
header ("Content-Type: image/png");
}
else
header ("Location: {$_SERVER['REQUEST_URI']}");
}
}