Backport asynchronous thumbnail generation from Kabuki.

This commit is contained in:
2017-12-20 14:51:23 +01:00
parent 981b652e25
commit 1def1484cb
9 changed files with 566 additions and 214 deletions

View File

@@ -76,11 +76,11 @@ class EditAsset extends HTMLController
$image->removeAllThumbnails();
}
}
elseif (preg_match('~^thumb_(\d+)x(\d+)(_c[best]?)?$~', $_POST['replacement_target']))
elseif (preg_match('~^thumb_(\d+x\d+(?:_c[best]?)?)$~', $_POST['replacement_target'], $match))
{
$image = $asset->getImage();
if (($replace_result = $image->replaceThumbnail($_POST['replacement_target'], $_FILES['replacement']['tmp_name'])) !== 0)
throw new Exception('Could not replace thumbnail \'' . $_POST['replacement_target'] . '\' with the uploaded file. Error code: ' . $replace_result);
if (($replace_result = $image->replaceThumbnail($match[1], $_FILES['replacement']['tmp_name'])) !== 0)
throw new Exception('Could not replace thumbnail \'' . $match[1] . '\' with the uploaded file. Error code: ' . $replace_result);
}
}
@@ -97,12 +97,18 @@ class EditAsset extends HTMLController
private function getThumbs(Asset $asset)
{
$path = $asset->getPath();
if (!$asset->isImage())
return [];
$image = $asset->getImage();
$subdir = $image->getSubdir();
$metadata = $image->getMeta();
$thumb_selectors = $image->getThumbnails();
$thumbs = [];
$metadata = $asset->getMeta();
foreach ($metadata as $key => $meta)
foreach ($thumb_selectors as $selector => $filename)
{
if (!preg_match('~^thumb_(?<width>\d+)x(?<height>\d+)(?<suffix>_c(?<method>[best]?))?$~', $key, $thumb))
if (!preg_match('~^(?<width>\d+)x(?<height>\d+)(?<suffix>_c(?<method>[best]?))?$~', $selector, $thumb))
continue;
$has_crop_boundary = isset($metadata['crop_' . $thumb['width'] . 'x' . $thumb['height']]);
@@ -113,10 +119,10 @@ class EditAsset extends HTMLController
'crop_method' => !$has_custom_image && !empty($thumb['method']) ? $thumb['method'] : (!empty($thumb['suffix']) ? 'c' : null),
'crop_region' => $has_crop_boundary ? $metadata['crop_' . $thumb['width'] . 'x' . $thumb['height']] : null,
'custom_image' => $has_custom_image,
'filename' => $meta,
'full_path' => THUMBSDIR . '/' . $path . '/' . $meta,
'url' => THUMBSURL . '/' . $path . '/' . $meta,
'status' => file_exists(THUMBSDIR . '/' . $path . '/' . $meta),
'filename' => $filename,
'full_path' => THUMBSDIR . '/' . $subdir . '/' . $filename,
'url' => THUMBSURL . '/' . $subdir . '/' . $filename,
'status' => file_exists(THUMBSDIR . '/' . $subdir . '/' . $filename),
];
}

View File

@@ -0,0 +1,27 @@
<?php
/*****************************************************************************
* GenerateThumbnail.php
* Contains the asynchronous thumbnail generation controller
*
* Kabuki CMS (C) 2013-2017, Aaron van Geffen
*****************************************************************************/
class GenerateThumbnail extends HTMLController
{
public function __construct()
{
$asset = Asset::fromId($_GET['id']);
if (empty($asset) || !$asset->isImage())
throw new NotFoundException('Image not found');
$image = $asset->getImage();
$crop_mode = isset($_GET['mode']) ? $_GET['mode'] : false;
$url = $image->getThumbnailUrl($_GET['width'], $_GET['height'], $crop_mode, true, true);
if ($url)
{
header('Location: ' . $url);
exit;
}
}
}