- Модуль: main
- Путь к файлу: ~/bitrix/modules/main/lib/File/Image/Gd.php
- Класс: BitrixMainFileImageGd
- Вызов: Gd::drawImageWatermark
public function drawImageWatermark(ImageWatermark $watermark)
{
if ($this->resource === null)
{
return false;
}
if (($image = $this->loadWatermark($watermark)) === null)
{
return false;
}
$width = $this->getWidth();
$height = $this->getHeight();
$watermarkWidth = $image->getWidth();
$watermarkHeight = $image->getHeight();
$position = new Rectangle($watermarkWidth, $watermarkHeight);
$watermark->alignPosition($width, $height, $position);
$watermarkX = $position->getX();
$watermarkY = $position->getY();
$watermarkAlpha = $watermark->getAlpha();
$repeat = ($watermark->getMode() == ImageWatermark::MODE_REPEAT);
for ($y = 0; $y < $watermarkHeight; $y++)
{
for ($x = 0; $x < $watermarkWidth; $x++)
{
$posY = $watermarkY + $y;
while (true)
{
$posX = $watermarkX + $x;
while (true)
{
$alpha = $watermarkAlpha;
$mainRgb = imagecolorsforindex($this->resource, imagecolorat($this->resource, $posX, $posY));
$watermarkRgb = imagecolorsforindex($image->resource, imagecolorat($image->resource, $x, $y));
if ($watermarkRgb['alpha'] == 127)
{
$pixel = $mainRgb;
}
else
{
if ($watermarkRgb['alpha'])
{
$alpha = round((( 127 - $watermarkRgb['alpha']) / 127), 2);
$alpha = $alpha * $watermarkAlpha;
}
$pixel = [];
foreach (['red', 'green', 'blue', 'alpha'] as $k)
{
$pixel[$k] = round(($mainRgb[$k] * (1 - $alpha)) + ($watermarkRgb[$k] * $alpha));
}
}
$color = imagecolorexactalpha($this->resource, $pixel['red'], $pixel['green'], $pixel['blue'], $pixel['alpha']);
if ($color == -1)
{
$color = imagecolorallocatealpha($this->resource, $pixel['red'], $pixel['green'], $pixel['blue'], $pixel['alpha']);
if ($color === false)
{
$color = imagecolorclosestalpha($this->resource, $pixel['red'], $pixel['green'], $pixel['blue'], $pixel['alpha']);
}
}
imagesetpixel($this->resource, $posX, $posY, $color);
$posX += $watermarkWidth;
if (!$repeat || $posX > $width)
{
break;
}
}
$posY += $watermarkHeight;
if (!$repeat || $posY > $height)
{
break;
}
}
}
}
$image->clear();
return true;
}