0
1
Fork 0
hedera-web-mindshore/rest/image/thumb.php

93 lines
2.2 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
2018-05-23 10:14:20 +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
2017-05-08 15:54:35 +00:00
*/
2018-05-23 10:14:20 +00:00
class Thumb extends Vn\Web\RestRequest {
function run() {
2016-07-22 20:00:27 +00:00
// XXX: Uncomment only to test the script
//$_SERVER['REQUEST_URI'] = 'catalog/200x200/e_cinerea.png';
2018-05-23 10:14:20 +00:00
$db = $this->getSysConn();
2016-07-22 20:00:27 +00:00
// Gets parameters from URI.
2018-05-23 10:14:20 +00:00
$uriSplit = explode('/', $_SERVER['REQUEST_URI']);
$uriSplit = array_slice($uriSplit, count($uriSplit) - 3, 3);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if (count($uriSplit) < 3)
throw new Exception('Bad request');
2016-07-22 20:00:27 +00:00
$schema = $uriSplit[0];
$orgFile = $uriSplit[2];
$file = $orgFile;
2018-05-23 10:14:20 +00:00
if (strrpos($file, '.') === FALSE)
2016-07-22 20:00:27 +00:00
$file .= '.png';
2018-05-23 10:14:20 +00:00
$size = explode('x', $uriSplit[1]);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if (count($size) < 2)
throw new Exception('Bad request');
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$width =(int) $size[0];
$height =(int) $size[1];
2016-07-22 20:00:27 +00:00
// Verifies that it is an allowed size.
$params = [
'schema' => $schema
,'width' => $width
,'height' => $height
];
2018-05-23 10:14:20 +00:00
$row = $db->getValue(
2016-07-22 20:00:27 +00:00
'SELECT crop
2017-12-20 11:34:04 +00:00
FROM imageCollection s
JOIN imageCollectionSize z ON z.collectionFk = s.id
2016-07-22 20:00:27 +00:00
WHERE s.name = #schema
AND z.width = #width
AND z.height = #height'
,$params
);
2018-05-23 10:14:20 +00:00
if (!isset($row))
throw new Exception('Size not allowed');
2016-07-22 20:00:27 +00:00
// Creates the thumb.
2016-08-25 10:47:09 +00:00
2018-05-23 10:14:20 +00:00
$util = new Util($this->app);
2016-08-25 10:47:09 +00:00
$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";
2018-05-23 10:14:20 +00:00
if (!file_exists($srcFile))
throw new Exception('Source not exists');
if (file_exists($dstFile))
throw new Exception('Destination already exists');
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$image = Image::create($srcFile);
Image::resizeSave($image, $dstFile, $height, $width, $row, $symbolicSrc);
imagedestroy($image);
2016-07-22 20:00:27 +00:00
// Sends the thumb to the client
2018-05-23 10:14:20 +00:00
$useXsendfile = $db->getValue('SELECT useXsendfile FROM imageConfig');
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if ($useXsendfile) {
header("X-Sendfile: $dstFile");
header("Content-Type: image/png");
2018-05-23 11:09:55 +00:00
} else
2018-05-23 10:14:20 +00:00
header("Location: {$_SERVER['REQUEST_URI']}");
2016-07-22 20:00:27 +00:00
}
}