fix(#103): apply dithering if requested by markup

This commit is contained in:
Benjamin Nussbaum 2025-10-29 22:35:16 +01:00
parent 315fbac261
commit 38e1b6f2a6
3 changed files with 39 additions and 8 deletions

View file

@ -72,6 +72,12 @@ class ImageGenerationService
->offsetY($imageSettings['offset_y'])
->outputPath($outputPath);
// Apply dithering if requested by markup
$shouldDither = self::markupContainsDitherImage($markup);
if ($shouldDither) {
$imageStage->dither();
}
(new TrmnlPipeline())->pipe($browserStage)
->pipe($imageStage)
->process();
@ -209,6 +215,31 @@ class ImageGenerationService
};
}
/**
* Detect whether the provided HTML markup contains an <img> tag with class "image-dither".
*/
private static function markupContainsDitherImage(string $markup): bool
{
if (mb_trim($markup) === '') {
return false;
}
// Find <img ... class="..."> (or with single quotes) and inspect class tokens
$imgWithClassPattern = '/<img\b[^>]*\bclass\s*=\s*(["\'])(.*?)\1[^>]*>/i';
if (! preg_match_all($imgWithClassPattern, $markup, $matches)) {
return false;
}
foreach ($matches[2] as $classValue) {
// Look for class token 'image-dither' or 'image--dither'
if (preg_match('/(?:^|\s)image--?dither(?:\s|$)/', $classValue)) {
return true;
}
}
return false;
}
public static function cleanupFolder(): void
{
$activeDeviceImageUuids = Device::pluck('current_screen_image')->filter()->toArray();