Home > Article > Backend Development > php图片验证码跟水印(php100教程33-34-35)
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_gdsession_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 "验证码输入错误!"; }}?>
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: [email protected]($image); break; case 2: [email protected]($image); break; case 3: [email protected]($image); break;}$waterimg='csdn.jpg';$img2=getimagesize($image);switch ($img2[2]){ case 1: [email protected]($waterimg); break; case 2: [email protected]($waterimg); break; case 3: [email protected]($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);?>