PHP Captcha与颜色保护

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Captcha与颜色保护相关的知识,希望对你有一定的参考价值。

session_start();
header("Content-type: image/png");
$width = 146;
$height = 30;

$im = @imagecreate($width, $height)or die("Cannot Initialize new GD image stream");

imagecolorallocate($im, 255, 250, 255);
$noise_color = imagecolorallocate($im, 207, 239, 250);
for($i=0; $i<($width*$height)/3; $i++) {
	imagefilledellipse($im, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}

/* generate random lines in background */
for($i=0; $i<($width*$height)/150; $i++ ) {
 imageline($im, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}

$text_color[0] = imagecolorallocate($im, 255, 0, 0);
$text_color[1] = imagecolorallocate($im, 51, 166, 207);

$text = mb_substr(uniqid(mt_rand(), true), 0, 6);
unset($_SESSION['captcha']);
$_SESSION['captcha']['blue'] = $_SESSION['captcha']['red'] = '';

for($j = 0; $j < mb_strlen($text); $j++) {
	imagettftext($im, 20, 0, 5+($j*23), 24, $text_color[$j%2], './offshore.ttf', $text[$j]);
	if(($j%2) == 1) {
		$_SESSION['captcha']['blue'] .= $text[$j];
	}
	else {
		$_SESSION['captcha']['red'] .= $text[$j];
	}
}
imagepng($im);
imagedestroy($im);

以上是关于PHP Captcha与颜色保护的主要内容,如果未能解决你的问题,请参考以下文章

Captcha.class.php

PHP 简单的PHP CAPTCHA

php TX Captcha代码

PHP 简单的图像CAPTCHA

php 在CodeIgniter网站中添加表单中的Captcha:https://www.cloudways.com/blog/captcha-in-codeigniter/

PHP Captcha 在服务器上不起作用,但在本地工作