Redirect first watermark request to generated static cache

This commit is contained in:
Terranom674
2026-08-15 19:12:57 +02:00
parent 7ae473bc61
commit 0f9a107bf9

View File

@@ -38,6 +38,7 @@ function bratonien_tools_watermark_fail($code, $message)
function bratonien_tools_output_image($path)
{
clearstatcache(true, $path);
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$types = array('jpg'=>'image/jpeg','jpeg'=>'image/jpeg','png'=>'image/png','gif'=>'image/gif','webp'=>'image/webp');
$mtime = @filemtime($path) ?: time();
@@ -63,6 +64,13 @@ function bratonien_tools_output_image($path)
exit;
}
function bratonien_tools_redirect_to_cache($url)
{
header('Cache-Control: no-store, max-age=0');
header('Location: '.$url, true, 302);
exit;
}
function bratonien_tools_url_to_size($value)
{
$parts = explode('x', (string)$value, 2);
@@ -249,12 +257,10 @@ function bratonien_tools_ensure_piwigo_derivative(array $derivative)
$image->sharpen($params->sharpen);
}
// Bewusst kein natives Piwigo-Wasserzeichen. Die Bratonien-Engine hält
// WatermarkParams.file leer und use_watermark=false. Dieser Cache bleibt
// damit auch für private Albumkontexte unverändert nutzbar.
$image->write($path);
$image->destroy();
@chmod($path, 0644);
clearstatcache(true, $path);
return is_file($path) && is_readable($path) ? $path : null;
}
@@ -319,7 +325,7 @@ if (!$descriptor)
if (is_file($descriptor['path']) && is_readable($descriptor['path']))
{
bratonien_tools_output_image($descriptor['path']);
bratonien_tools_redirect_to_cache($descriptor['url']);
}
if (!is_dir($descriptor['dir']) && !mkdir($descriptor['dir'], 0755, true) && !is_dir($descriptor['dir']))
@@ -342,7 +348,7 @@ if (is_file($descriptor['path']) && is_readable($descriptor['path']))
{
flock($lock, LOCK_UN);
fclose($lock);
bratonien_tools_output_image($descriptor['path']);
bratonien_tools_redirect_to_cache($descriptor['url']);
}
$piwigo_derivative = bratonien_tools_ensure_piwigo_derivative($derivative);
@@ -416,8 +422,14 @@ if ($width >= (int)$profile['min_width'] && $height >= (int)$profile['min_height
$image->write($descriptor['path']);
$image->destroy();
@chmod($descriptor['path'], 0644);
clearstatcache(true, $descriptor['path']);
flock($lock, LOCK_UN);
fclose($lock);
bratonien_tools_output_image($descriptor['path']);
if (!is_file($descriptor['path']) || !is_readable($descriptor['path']))
{
bratonien_tools_watermark_fail(500, 'Generated watermark cache unavailable');
}
bratonien_tools_redirect_to_cache($descriptor['url']);