Каптчи

Опубликовано 8 января, 2009 в PHP

Надеюсь, все знают что такое каптча. Очень давно написал несколько каптч, теперь выложу сюда, чтобы не потерять их. Код ниже объединяет сразу 3 каптчи, которые выводятся рандомно.

<?php
session_start();
$chars = array(1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','R','S','T','U','V','W','X','Y','Z');

$CreateImage = imageCreate(150, 50);

$color = imageColorAllocate($CreateImage, 255, 255, 255);
imagefill($CreateImage, 0, 0, $color);

$TrueChars = array_rand($chars, 5);
$_SESSION['TrueChars'] = '';

$rand = rand(0, 2);

if($rand == 0) {
	for($y = 0; $y <= imageSY($CreateImage); $y += rand(1, 2)) {
		$color1 = rand(0, 200);
		$color2 = rand(0, 200);
		$color3 = rand(0, 200);

		$color = imageColorAllocate($CreateImage, 0, 0, 0);
		imageLine($CreateImage, 0, $y, imageSX($CreateImage), $y+rand(-1, 1), $color);
	}

	for($x = 0; $x <= imageSX($CreateImage); $x += rand(1, 8)) {
		$color1 = rand(0, 200);
		$color2 = rand(0, 200);
		$color3 = rand(0, 200);

		$color = imageColorAllocate($CreateImage, 0, 0, 0);
		imageLine($CreateImage, $x, 0, $x+rand(-1, 1), imageSY($CreateImage), $color);
	}

	$CharX = 0;
	foreach($TrueChars as $TrueChar) {
		$_SESSION['TrueChars'] .= $chars[$TrueChar];
		$CharX = $CharX+rand(20, 25);
		$CharY = rand(21, 40);
		$ChSize = rand(17, 21);
		$ChSk = rand(-20, 20);

		$color = imageColorAllocate($CreateImage, 255, 255, 255);
		imageTtfText($CreateImage, $ChSize, $ChSk, $CharX, $CharY, $color, 'DREAMOFM.TTF', $chars[$TrueChar]);
	}
}
elseif($rand == 1) {
	$CharX = 0;
	foreach($TrueChars as $TrueChar) {
		$_SESSION['TrueChars'] .= $chars[$TrueChar];
		$CharX = $CharX+rand(20, 25);
		$CharY = rand(21, 35);

		$CharX2 = $CharX+rand(0, 6);
		$CharY2 = $CharY+rand(-3, 3); 

		$ChSize = rand(15, 21);
		$ChSk = rand(-25, 25);
		$color1 = rand(0, 100);
		$color2 = rand(0, 100);
		$color3 = rand(0, 100);

		$color2_1 = rand(-100, 100);
		$color2_2 = rand(-100, 100);
		$color2_3 = rand(-100, 100);

		$color = imageColorAllocate($CreateImage, $color1, $color2, $color3);
		$color2 = imageColorAllocate($CreateImage, ($color1+$color2_1), ($color2+$color2_2), ($color3+$color2_3));
		imageTtfText($CreateImage, $ChSize, $ChSk, $CharX, $CharY, $color, 'DREAMOFM.TTF', $chars[$TrueChar]);
		imageTtfText($CreateImage, $ChSize, $ChSk, $CharX2, $CharY2, $color2, 'DREAMOFM.TTF', $chars[$TrueChar]);
	}
}
else {
	for($i = 0; $i <= 10; $i++) {
		$lineX1 = rand(0, 100);
		$lineY1 = rand(0, 50);
		$lineX2 = $lineX1+rand(20, 50);
		$lineY2 = $lineY1-rand(0, 50);

		$color1 = rand(150, 255);
		$color2 = rand(150, 255);
		$color3 = rand(150, 255);
		$color = imageColorAllocate($CreateImage, $color1, $color2, $color3);

 		imageFilledRectangle($CreateImage, $lineX1, $lineY1, $lineX2, $lineY2, $color);
	}

	$CharX = 0;

	foreach($TrueChars as $TrueChar) {
		$_SESSION['TrueChars'] .= $chars[$TrueChar];
		$CharX = $CharX+rand(20, 25);
		$CharY = rand(21, 35);
		$ChSize = rand(15, 21);
		$ChSk = rand(-25, 25);
		$color1 = rand(0, 100);
		$color2 = rand(0, 100);
		$color3 = rand(0, 100);

		$color = imageColorAllocate($CreateImage, $color1, $color2, $color3);
		imageTtfText($CreateImage, $ChSize, $ChSk, $CharX, $CharY, $color, 'DREAMOFM.TTF', $chars[$TrueChar]);
	}

	for($i = 0; $i <= 3; $i++) {
		$lineX1 = rand(0, 100);
		$lineY1 = rand(0, 50);
		$lineX2 = rand(100, 150);
		$lineY2 = rand(0, 50);
		$color = imageColorAllocate($CreateImage, 255, 255, 255);
		imageLine($CreateImage, $lineX1, $lineY1, $lineX2, $lineX2, $color);

		$lineX1 = rand(100, 150);
		$lineY1 = rand(0, 50);
		$lineX2 = rand(0, 100);
		$lineY2 = rand(0, 50);
		$color = imageColorAllocate($CreateImage, rand(0, 255), rand(0, 255), rand(0, 255));
		imageLine($CreateImage, $lineX1, $lineY1, $lineX2, $lineX2, $color);
	}

	for($i = 0; $i <= 8; $i++) {
		$lineX1 = rand(100, 150);
		$lineY1 = rand(0, 50);
		$lineX2 = rand(0, 100);
		$lineY2 = rand(0, 50);
		$color = imageColorAllocate($CreateImage, 255, 255, 255);
		imageLine($CreateImage, $lineX1, $lineY1, $lineX2, $lineX2, $color);
		imageFilledRectangle($CreateImage, $lineX1+5, $lineY1-5, $lineX1+5-1, $lineY1-5-1, $color);
	}
}

Header("Content-Type: image/Jpeg");
imageJpeg($CreateImage);
imageDestroy($CreateImage);
?>

Ложим данный код в любой файл с расширением .php, запускаем его через <img src=»имя_файла.php» alt=»" />. Правильное значение сохраняется в сессии $_SESSION['TrueChars'].

PS: чуть не забыл загрузить используемый шрифт. dreamofm шрифт

PPS: Вот пример каптчи: Пример каптчи (обновить изображение)




Комментарии «Каптчи»:
Нет комментариев. Будете первыми?
Ваш комментарий: