Precache standard and custom Piwigo derivative sizes

This commit is contained in:
Terranom674
2026-08-15 16:55:12 +02:00
parent 123662a3f8
commit 7f4d10aed7

View File

@@ -18,7 +18,7 @@ $_SERVER['REQUEST_URI'] = '/';
$_SERVER['SCRIPT_NAME'] = '/plugins/bratonien_tools/precache.php'; $_SERVER['SCRIPT_NAME'] = '/plugins/bratonien_tools/precache.php';
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME']; $_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
$_SERVER['QUERY_STRING'] = ''; $_SERVER['QUERY_STRING'] = '';
$_SERVER['HTTP_USER_AGENT'] = 'Bratonien-Watermark-Precache/1.0'; $_SERVER['HTTP_USER_AGENT'] = 'Bratonien-Watermark-Precache/1.1';
$_SERVER['HTTPS'] = 'off'; $_SERVER['HTTPS'] = 'off';
require_once(PHPWG_ROOT_PATH.'include/common.inc.php'); require_once(PHPWG_ROOT_PATH.'include/common.inc.php');
@@ -32,6 +32,55 @@ if (!defined('BRATONIEN_TOOLS_PATH'))
require_once(BRATONIEN_TOOLS_PATH.'include/watermark_runtime.inc.php'); require_once(BRATONIEN_TOOLS_PATH.'include/watermark_runtime.inc.php');
require_once(PHPWG_ROOT_PATH.'include/derivative.inc.php'); require_once(PHPWG_ROOT_PATH.'include/derivative.inc.php');
function bratonien_tools_precache_url_to_size($value)
{
$parts = explode('x', (string)$value, 2);
if (count($parts) === 1)
{
$size = max(1, (int)$parts[0]);
return array($size, $size);
}
return array(max(1, (int)$parts[0]), max(1, (int)$parts[1]));
}
function bratonien_tools_precache_custom_params($key)
{
$tokens = explode('_', (string)$key);
if (!$tokens)
{
return null;
}
$token = array_shift($tokens);
$crop = 0;
$min_size = null;
if (isset($token[0]) && $token[0] === 's')
{
$size = bratonien_tools_precache_url_to_size(substr($token, 1));
}
elseif (isset($token[0]) && $token[0] === 'e')
{
$crop = 1;
$size = $min_size = bratonien_tools_precache_url_to_size(substr($token, 1));
}
else
{
if (count($tokens) < 2)
{
return null;
}
$size = bratonien_tools_precache_url_to_size($token);
$crop_token = array_shift($tokens);
$min_size = bratonien_tools_precache_url_to_size(array_shift($tokens));
$crop = char_to_fraction($crop_token);
}
$params = new DerivativeParams(new SizingParams($size, $crop, $min_size));
ImageStdParams::apply_global($params);
return $params;
}
if (!bratonien_tools_watermark_engine_enabled()) if (!bratonien_tools_watermark_engine_enabled())
{ {
echo "Wasserzeichen-Engine ist deaktiviert; kein Precache notwendig.\n"; echo "Wasserzeichen-Engine ist deaktiviert; kein Precache notwendig.\n";
@@ -80,10 +129,21 @@ while ($row = pwg_db_fetch_assoc($result))
$image_categories[$image_id][] = (int)$row['category_id']; $image_categories[$image_id][] = (int)$row['category_id'];
} }
$root_url = get_absolute_root_url(); $variants = array();
$endpoint = rtrim($root_url, '/').'/plugins/'.BRATONIEN_TOOLS_ID.'/watermark.php'; foreach (ImageStdParams::get_defined_type_map() as $type => $params)
$types = ImageStdParams::get_defined_type_map(); {
$variants[$type] = $params;
}
foreach (ImageStdParams::$custom as $custom_key => $last_used)
{
$params = bratonien_tools_precache_custom_params($custom_key);
if ($params)
{
$variants['custom:'.$custom_key] = $params;
}
}
$endpoint = 'http://127.0.0.1/plugins/'.BRATONIEN_TOOLS_ID.'/watermark.php';
$checked = 0; $checked = 0;
$generated = 0; $generated = 0;
$already_cached = 0; $already_cached = 0;
@@ -123,7 +183,7 @@ while ($image = pwg_db_fetch_assoc($result))
{ {
$profile = $profiles[$profile_id]; $profile = $profiles[$profile_id];
foreach ($types as $type => $params) foreach ($variants as $variant_name => $params)
{ {
$checked++; $checked++;
@@ -183,7 +243,7 @@ while ($image = pwg_db_fetch_assoc($result))
CURLOPT_CONNECTTIMEOUT => 3, CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 120, CURLOPT_TIMEOUT => 120,
CURLOPT_FAILONERROR => false, CURLOPT_FAILONERROR => false,
CURLOPT_USERAGENT => 'Bratonien-Watermark-Precache/1.0', CURLOPT_USERAGENT => 'Bratonien-Watermark-Precache/1.1',
CURLOPT_WRITEFUNCTION => static function ($ch, $data) { return strlen($data); }, CURLOPT_WRITEFUNCTION => static function ($ch, $data) { return strlen($data); },
)); ));
curl_exec($ch); curl_exec($ch);
@@ -193,7 +253,7 @@ while ($image = pwg_db_fetch_assoc($result))
$ok = ($status >= 200 && $status < 300 && is_file($descriptor['path'])); $ok = ($status >= 200 && $status < 300 && is_file($descriptor['path']));
if (!$ok) if (!$ok)
{ {
fwrite(STDERR, "Bild #$image_id / Profil #$profile_id / $type: Precache fehlgeschlagen (HTTP $status".($curl_error !== '' ? ", $curl_error" : '').").\n"); fwrite(STDERR, "Bild #$image_id / Profil #$profile_id / $variant_name: Precache fehlgeschlagen (HTTP $status".($curl_error !== '' ? ", $curl_error" : '').").\n");
} }
} }
else else
@@ -203,7 +263,7 @@ while ($image = pwg_db_fetch_assoc($result))
$ok = is_file($descriptor['path']); $ok = is_file($descriptor['path']);
if (!$ok) if (!$ok)
{ {
fwrite(STDERR, "Bild #$image_id / Profil #$profile_id / $type: Precache fehlgeschlagen.\n"); fwrite(STDERR, "Bild #$image_id / Profil #$profile_id / $variant_name: Precache fehlgeschlagen.\n");
} }
} }