php生成酷炫的四个字符验证码

yipeiwu_com6年前PHP代码库

本文实例为大家分享php生成验证码的实现代码,供大家参考,具体内容如下

<?php
$im=imagecreate(200,100);//生成画布
imagecolorallocate($im,0,0,0);//背景色
$white=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//生成随机颜色
for($i=0;$i<9;$i++)
{
 imageline($im,rand(0,200),rand(0,100),rand(0,200),rand(0,100),$white);//生成干扰线条元素
}
for($i=0;$i<150;$i++)
{
 imagesetpixel($im,rand(0,200),rand(0,100),$white);//生成干扰点元素
}
for($i=0,$str='';$i<4;$i++)//通过循环获得四个字符
{
 switch(rand(1,3))
 {
  case'1':
  $ch=rand(0,9);
  break; 
  case'2':
  $ch=sprintf('%c',rand(97,122));
  break;
  case'3':
  $ch=sprintf('%c',rand(65,90));
  break;
 }
  $str.=$ch;

}
imagettftext($im,32,rand(0,15),55,70,$white,'c.ttc',$str);//在画布上输出字符串
header("Content-type:image/jpeg");
imagejpeg($im);
imagedestroy($im);
?>

以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助。

相关文章

php数字每三位加逗号的功能函数

php实现数字格式化,数字每三位加逗号的功能函数,具体代码如下: function num_format($num){ if(!is_numeric($num)){ re...

基于PHP常用字符串的总结(待续)

1.分割与合并implode:echo implode(",", array('lastname', 'email', 'phone'));//数组转成字符串explode:print_...

PHP技术开发技巧分享

1. 提高PHP的运行效率   PHP的优点之一是速度很快,对于一般的网站应用,可以说是已经足够了。不过如果站点的访问量很高、带宽窄或者其它的因素令服务器产生性能瓶颈的时候,你可能得想想...

WordPress中调试缩略图的相关PHP函数使用解析

the_post_thumbnail the_post_thumbnail 在 WordPress 中主要用来打印文章中设定的缩略图,而 get_the_post_thumbnail 函...

PHP封装cURL工具类与应用示例

本文实例讲述了PHP封装cURL工具类。分享给大家供大家参考,具体如下: CurlUtils工具类: <?php /** * cURL请求工具类 */ class...