* Vykreslovani obrazku uploadovanych na server
*
* @author psotona
*/
try {
$imgDirKey = $_SERVER['HTTP_HOST'].'-'.IMG_DIR_KEY;
/** @var ImageService $imageService */
$imageService = null;
// if (APC_EXTENSION_LOADED) {
// if (apc_exists($imgDirKey)) {
// $imageFolder = apc_fetch($imgDirKey);
// $imageService = new ImageService($imageFolder);
// }
// }
if (!$imageService) {
require_once __DIR__.'/../app/bootstrap.php';
$imageService = $container->getService('imageRewriteService');
if (APC_EXTENSION_LOADED) {
$imageFolder = $imageService->getImageFolder();
apc_store($imgDirKey, $imageFolder, 30);
}
}
//z url ziskame pozadovanou sirku, vysku obrazku a cestu k souboru
$matches = array();
$url = $_SERVER['REQUEST_URI'];
// localhost fix
$tmp = $_SERVER['PHP_SELF'];
$tmp = substr($tmp, 0, strrpos($tmp, '/'));
$len = strlen($tmp);
if ($tmp == substr($url, 0, $len))
{
$url = substr($url, $len);
}
$patternOld = '/^\/rw-img(-h)?(?P[0-9]+)?(-w)?(?P[0-9]+)?\/(?P.+)$/';
$patternNew = '~^/rw-img/(?P\d+)/(?P\d+)/(?P.+)$~';
if (!preg_match($patternNew, $url, $matches))
{
if (preg_match($patternOld, $url, $matches))
{
$url = $imageService->getThumbUrl($matches['file'], $matches['width'], $matches['height']);
header('Location: ' . $url, true, 301);
}
exit;
}
$height = $matches['height'];
$width = $matches['width'];
$file = urldecode($matches['file']);
//odstranime pokusy dostat se z adresarove struktury
$file = str_replace('..', '', $file);
// remove ?query
$file = parse_url($file, PHP_URL_PATH);
// Nette neumi pracovat s ICO
$filePath = $imageService->getFilePath($file);
$info = @getimagesize($imageService->getFilePath($file));
if ($info && ($info[2] == IMAGETYPE_ICO || $height + $width == 0))
{
$tmp = $imageService->getCacheFilePath($filePath);
$imageService->createDir($tmp);
copy($filePath, $tmp);
header('Content-type: image/x-icon');
readfile($filePath);
exit;
}
$image = $imageService->getImage($file, $width, $height);
if ($image)
{
// Nette neumi udelat detekci pri generovani a ve vychozim generuje JPG (nefunguje pruhlednost PNG)
$type = NULL;
switch (strtolower(pathinfo($file, PATHINFO_EXTENSION)))
{
case 'jpg':
case 'jpeg':
$type = Image::JPEG;
break;
case 'gif':
$type = Image::GIF;
break;
default:
$type = Image::PNG;
}
$image->send($type);
return;
}
} catch(\Exception $e) {
//nothing to do
}
//existuje vychozi soubor?
header("HTTP/1.0 404 Not Found");