当前位置: 代码迷 >> PHP >> php图片验证码跟水印(php100教程33-34-35)
  详细解决方案

php图片验证码跟水印(php100教程33-34-35)

热度:318   发布时间:2013-02-03 12:33:31.0
php图片验证码和水印(php100教程33-34-35)

1.验证码的制作:

che.php

<?php
/*
 *Create on 2013-1-29
 *This PHP file is made by ZhengHaibo
 *blog.csdn.net/nuptboyzhb
 */
//首先要修改php.ini 打开图像处理的库php_gd
session_start();//启动session,用于保存验证码
for($i=0;$i<4;$i++){
	$rand.=dechex(rand(1,15));
}
$_SESSION[check_pic]=$rand;//保存验证码
$im=imagecreatetruecolor(100, 30);
//设置颜色
$bg=imagecolorallocate($im, 150, 150, 150);
$te=imagecolorallocate($im, 255, 255, 255);
//画线条 干扰线条
for ($j = 0; $j <5; $j++) {
	$te2 = imagecolorallocate($im, rand(0,255),rand(0,255),rand(0,255));
	imageline($im,rand(0,100),rand(0,30),rand(0,100),rand(0,30),$te2);
}
for ($j2 = 0; $j2 < number_variable; $j2++) {
	imagesetpixel($im, rand()%100, rand()%30,$te2);
}
//把字符串写到图像的左上角
imagestring($im,6,rand(3,70),rand(0,16),$rand,$te);
//输入文字
header("Content-type: image/jpeg");
imagejpeg($im);
?>


index.php

<?php
/*
 *Create on 2013-1-29
 *This PHP file is made by ZhengHaibo
 *blog.csdn.net/nuptboyzhb
 */
session_start();
if ($_POST['submit']) {
	if ($_POST['check']==$_SESSION[check_pic]) {
		echo "验证码输入正确".$_SESSION[check_pic];
	}else{
		echo "验证码输入错误!";
	}
}
?>
<form action="" method="post">
<img src='che.php'><br>
<input type="text" name="check"><br>
<input type="submit" name="submit" value="提交"><br>
</form>


2.水印的制作

index.php

<?php
/*
 *Create on 2013-1-29
 *This PHP file is made by ZhengHaibo
 *blog.csdn.net/nuptboyzhb
 */
//首先要修改php.ini 打开图像处理的库php_gd
$image='nupt.jpg';
$img=getimagesize($image);
switch ($img[2])
{
	case 1:
		$im=@imagecreatefromgif($image);
		break;
	case 2:
		$im=@imagecreatefromjpeg($image);
		break;
	case 3:
		$im=@imagecreatefrompng($image);
		break;
}
$waterimg='csdn.jpg';
$img2=getimagesize($image);
switch ($img2[2])
{
	case 1:
		$im2=@imagecreatefromgif($waterimg);
		break;
	case 2:
		$im2=@imagecreatefromjpeg($waterimg);
		break;
	case 3:
		$im2=@imagecreatefrompng($waterimg);
		break;
}
$str="http://blog.csdn.net/nuptboyzhb/";
$te=imagecolorallocate($im, 255, 255, 255);
//把字符串写到图像的左上角
imagestring($im,6,rand(3,70),rand(0,16),$str,$te);
//把水印图片显示到图像上
imagecopy($im, $im2, 0, 30, 0, 0, 40,40);
header("Content-type: image/jpeg");
imagejpeg($im);
?>