Adjust watermark permissions and transparency
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user