Use thumbnail/placeholder logic for imagebox

This commit is contained in:
2026-01-21 11:03:41 +01:00
parent e1d24c6627
commit 351485efb1
5 changed files with 275 additions and 70 deletions

View File

@@ -2,6 +2,7 @@
use dokuwiki\Extension\SyntaxPlugin;
use dokuwiki\plugin\luxtools\Path;
use dokuwiki\plugin\luxtools\ThumbnailHelper;
require_once(__DIR__ . '/../autoload.php');
@@ -58,7 +59,7 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
// ?200x150&center - full options
$width = null;
$height = null;
$align = 'right'; // default alignment
$align = null; // Will use default if not specified
if (strpos($pathPart, '?') !== false) {
[$pathPart, $paramStr] = explode('?', $pathPart, 2);
@@ -107,6 +108,18 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
return true;
}
// Apply default settings if not explicitly specified
if ($data['width'] === null) {
$data['width'] = (int)$this->getConf('default_image_width');
if ($data['width'] <= 0) $data['width'] = 250;
}
if ($data['align'] === null) {
$data['align'] = (string)$this->getConf('default_image_align');
if (!in_array($data['align'], ['left', 'right', 'center'], true)) {
$data['align'] = 'right';
}
}
try {
$pathHelper = new Path($this->getConf('paths'));
// Use addTrailingSlash=false since this is a file path, not a directory
@@ -135,14 +148,23 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
return true;
}
// Build the image URL using the plugin's file.php endpoint
// Get thumbnail from helper - it handles everything
global $ID;
$imageUrl = $this->buildFileUrl($pathInfo, $data['width'], $data['height']);
$placeholderId = trim((string)$this->getConf('thumb_placeholder'));
$thumb = ThumbnailHelper::getThumbnail(
$pathInfo['root'],
$pathInfo['local'],
$ID,
$data['width'] ?? 250,
$data['height'] ?? ($data['width'] ?? 250),
80,
$placeholderId !== '' ? $placeholderId : null
);
// Build full-size URL for linking
$fullUrl = $this->buildFileUrl($pathInfo, null, null);
$fullUrl = $this->buildFileUrl($pathInfo, null, null, false);
$this->renderImageBox($renderer, $imageUrl, $fullUrl, $data);
$this->renderImageBox($renderer, $thumb, $fullUrl, $data);
return true;
}
@@ -153,9 +175,10 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
* @param array $pathInfo Path info from Path helper
* @param int|null $width Optional width
* @param int|null $height Optional height
* @param bool $thumbnail Whether to generate a thumbnail
* @return string
*/
protected function buildFileUrl(array $pathInfo, ?int $width, ?int $height): string
protected function buildFileUrl(array $pathInfo, ?int $width, ?int $height, bool $thumbnail = false): string
{
global $ID;
@@ -165,6 +188,12 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
'id' => $ID,
];
if ($thumbnail && ($width !== null || $height !== null)) {
// Enable thumbnail mode (same as gallery logic)
$params['thumb'] = 1;
$params['q'] = 80; // JPEG quality
}
if ($width !== null) {
$params['w'] = $width;
}
@@ -179,11 +208,11 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
* Render the imagebox HTML.
*
* @param \Doku_Renderer $renderer
* @param string $imageUrl URL for the displayed image
* @param array $thumb Thumbnail info from ThumbnailHelper::getThumbnail()
* @param string $fullUrl URL for the full-size image (on click)
* @param array $data Parsed data from handle()
*/
protected function renderImageBox(\Doku_Renderer $renderer, string $imageUrl, string $fullUrl, array $data): void
protected function renderImageBox(\Doku_Renderer $renderer, array $thumb, string $fullUrl, array $data): void
{
$align = $data['align'] ?? 'right';
$caption = $data['caption'] ?? '';
@@ -205,8 +234,11 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
$outerStyle = ' style="width: ' . ($width + 10) . 'px;"';
}
// Use thumbnail metadata from helper
$dataThumbAttr = $thumb['isFinal'] ? '' : ' data-thumb-src="' . hsc($thumb['thumbUrl']) . '"';
// Build image attributes
$imgAttrs = 'class="media" loading="lazy" decoding="async"';
$imgAttrs = 'class="media luxtools-thumb" loading="lazy" decoding="async"';
if ($width !== null) {
$imgAttrs .= ' width="' . (int)$width . '"';
}
@@ -221,8 +253,8 @@ class syntax_plugin_luxtools_image extends SyntaxPlugin
// Image with link to full size
$renderer->doc .= '<a href="' . hsc($fullUrl) . '" class="media" target="_blank">';
$renderer->doc .= '<img src="' . hsc($imageUrl) . '" ' . $imgAttrs . ' />';
$renderer->doc .= '</a>';
$renderer->doc .= '<img src="' . hsc($thumb['url']) . '" ' . $imgAttrs . $dataThumbAttr . ' />';
$renderer->doc .= '</a>';;
// Caption
if ($caption !== '') {