- Модуль: main
- Путь к файлу: ~/bitrix/modules/main/lib/File/Image/Gd.php
- Класс: BitrixMainFileImageGd
- Вызов: Gd::drawTextWatermark
public function drawTextWatermark(TextWatermark $watermark)
{
if ($this->resource === null)
{
return false;
}
$font = $watermark->getFont();
if (!file_exists($font))
{
return false;
}
$utfText = $watermark->getUtfText();
$width = $this->getWidth();
$height = $this->getHeight();
if (($textWidth = $watermark->getWidth()) > 0)
{
$textBox = imagettfbbox(20, 0, $font, $utfText);
if (!is_array($textBox))
{
return false;
}
$scale = $textWidth / ($textBox[2] - $textBox[0]);
$fontSize = 20 * $scale;
$position = new Rectangle($textWidth, ($textBox[0] - $textBox[7]) * $scale);
}
else
{
$fontSize = $watermark->getFontSize($width);
$textBox = imagettfbbox($fontSize, 0, $font, $utfText);
$position = new Rectangle(($textBox[2] - $textBox[0]), ($textBox[0] - $textBox[7]));
}
$watermark->alignPosition($width, $height, $position);
$color = $watermark->getColor();
$textColor = imagecolorallocate($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue());
if ($watermark->getVerticalAlignment() == Watermark::ALIGN_BOTTOM)
{
// Try to take into consideration font's descenders.
// Coordinates in imagettftext are for font's *baseline*.
// Let the descenders be 20% of the font size.
$descender = $fontSize * 0.2;
$y = $position->getY() + $position->getHeight() - $descender; // baseline
}
else
{
$y = $position->getY() + $fontSize; // baseline
}
$result = imagettftext($this->resource, $fontSize, 0, $position->getX(), $y, $textColor, $font, $utfText);
return ($result !== false);
}