Adjust watermark permissions and transparency
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled

This commit is contained in:
Codex Agent
2026-01-19 13:45:43 +01:00
parent fbff2afa3e
commit d4ab9a3a20
15 changed files with 325 additions and 54 deletions

View File

@@ -178,10 +178,13 @@ class ImageHelper
$y = max(0, min($srcH - $targetH, $y + $offsetY));
$opacity = max(0.0, min(1.0, (float) ($config['opacity'] ?? 0.25)));
$mergeOpacity = (int) round($opacity * 100); // imagecopymerge uses 0-100
if ($opacity < 1.0) {
self::applyOpacity($resized, $opacity);
}
imagealphablending($src, true);
imagecopymerge($src, $resized, $x, $y, 0, 0, $targetW, $targetH, $mergeOpacity);
imagecopy($src, $resized, $x, $y, 0, 0, $targetW, $targetH);
imagedestroy($resized);
// Overwrite original (respect mime: always JPEG for compatibility)
@@ -210,4 +213,34 @@ class ImageHelper
return $applied ? $destPath : null;
}
/**
* @param \GdImage|resource $image
*/
private static function applyOpacity($image, float $opacity): void
{
$width = imagesx($image);
$height = imagesy($image);
if ($width <= 0 || $height <= 0) {
return;
}
imagealphablending($image, false);
imagesavealpha($image, true);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$rgba = imagecolorat($image, $x, $y);
$alpha = ($rgba >> 24) & 0x7F;
$red = ($rgba >> 16) & 0xFF;
$green = ($rgba >> 8) & 0xFF;
$blue = $rgba & 0xFF;
$adjustedAlpha = (int) round(127 - (127 - $alpha) * $opacity);
$color = imagecolorallocatealpha($image, $red, $green, $blue, max(0, min(127, $adjustedAlpha)));
imagesetpixel($image, $x, $y, $color);
}
}
}
}