• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/captcha.php
  • Класс: CCaptcha
  • Вызов: CCaptcha::DrawText
function DrawText()
{
	if ($this->bTransparentText)
		$alpha = floor($this->transparentTextPercent / 100 * 127);

	$yMin = ($this->imageHeight / 2) + ($this->textFontSize / 2) - 2;
	$yMax = ($this->imageHeight / 2) + ($this->textFontSize / 2) + 2;

	$bPrecise = $this->textDistanceFrom < 0 && $this->textDistanceTo < 0;

	if($bPrecise)
	{
		//We'll need inversed color to draw on background
		$bg_color_hex = $this->arRealBGColor[0] << 16 | $this->arRealBGColor[1] << 8 | $this->arRealBGColor[2];
		$not_bg_color = array(
			(!$bg_color_hex >> 16) & 0xFF,
			(!$bg_color_hex >> 8) & 0xFF,
			(!$bg_color_hex) & 0xFF
		);
	}

	$arPos = array();
	$x = 0;

	for ($i = 0; $i < $this->codeLength; $i++)
	{
		$char = mb_substr($this->code, $i, 1);
		$utf = BitrixMainTextEncoding::convertEncoding($char, LANG_CHARSET, "utf-8");

		$ttfFile = $_SERVER["DOCUMENT_ROOT"].$this->ttfFilesPath."/".$this->arTTFFiles[rand(1, count($this->arTTFFiles)) - 1];
		$angle = rand($this->textAngleFrom, $this->textAngleTo);

		$bounds = imagettfbbox($this->textFontSize, $angle, $ttfFile, $utf);

		$height = max($bounds[1], $bounds[3], $bounds[5], $bounds[7]) - min($bounds[1], $bounds[3], $bounds[5], $bounds[7]);
		$width = max($bounds[0], $bounds[2], $bounds[4], $bounds[6]) - min($bounds[0], $bounds[2], $bounds[4], $bounds[6]);

		$y = $height + rand(0, ($this->imageHeight-$height)*0.9);

		if($bPrecise)
		{
			//Now for precise positioning we need to draw characred and define it's borders
			$img = $this->InitImage($width, $this->imageHeight);
			$tmp = imagecolorallocate($img, $not_bg_color[0], $not_bg_color[1], $not_bg_color[2]);
			$dx = -min($bounds[0], $bounds[2], $bounds[4], $bounds[6]);
			imagettftext($img, $this->textFontSize, $angle, $dx, $y, $tmp, $ttfFile, $utf);

			$arLeftBounds = array();
			for($yy=0; $yy < $this->imageHeight; $yy++)
			{
				$arLeftBounds[$yy] = 0;
				for($xx=0; $xx < $width; $xx++)
				{
					$rgb = imagecolorat($img, $xx, $yy);
					if($rgb !== $bg_color_hex)
					{
						$arLeftBounds[$yy] = $xx;
						break;
					}
				}

				$arRightBounds[$yy] = 0;
				if($arLeftBounds[$yy] > 0)
				{
					for($xx=$width; $xx > 0; $xx--)
					{
						$rgb = imagecolorat($img, $xx-1, $yy);
						if($rgb !== $bg_color_hex)
						{
							$arRightBounds[$yy] = $xx-1;
							break;
						}
					}
				}
			}

			imagedestroy($img);
		}
		else
		{
			$arLeftBounds = array();
			$arRightBounds = array();
			$dx = 0;
		}

		if($i > 0)
		{
			if($bPrecise)
			{
				$arDX = array();
				for($yy=0; $yy < $this->imageHeight; $yy++)
				{
					if($arPos[$i-1][6][$yy] > 0 && $arLeftBounds[$yy] > 0)
						$arDX[$yy] = ($arPos[$i-1][6][$yy] - $arPos[$i-1][7])-($arLeftBounds[$yy]-$dx);
					else
						$arDX[$yy] = $arPos[$i-1][5][$yy] - $arPos[$i-1][7];
				}
				$x += max($arDX)+(rand($this->textDistanceFrom, $this->textDistanceTo));
			}
			else
			{
				$x += rand($this->textDistanceFrom, $this->textDistanceTo);
			}
		}
		else
		{
			$x = rand($this->textStartX/2, $this->textStartX*2);
		}

		$arPos[$i] = array(
			$angle,		//0
			$x,		//1
			$y,		//2
			$ttfFile,	//3
			$char,		//4
			$arLeftBounds,	//5
			$arRightBounds,	//6
			$dx,		//7
			$utf,		//8
		);

	}

	foreach($arPos as $pos)
	{
		$arTextColor = $this->GetColor($this->arTextColor);

		if ($this->bTransparentText)
			$color = imagecolorallocatealpha($this->image, $arTextColor[0], $arTextColor[1], $arTextColor[2], $alpha);
		else
			$color = imagecolorallocate($this->image, $arTextColor[0], $arTextColor[1], $arTextColor[2]);

		$bounds = imagettftext($this->image, $this->textFontSize, $pos[0], $pos[1], $pos[2], $color, $pos[3], $pos[8]);

		$x2 = $pos[1] + ($bounds[2] - $bounds[0]);
	}

	if($this->bEmptyText)
		$this->EmptyText();

	return $x2;
}