PHP GD库绘制验证码

PHP GD库绘制简单验证码

程序代码:

<?php

//简单使用GD绘制验证码   需要加载PHP_gd2.dll文件

// session_start();

/*

 *功能:给出随机长度的字符

 *参数:

 *            $len -给定字符串的长度

 *输出:随机给定长度的字符串

 * */

header ( "content-type:image/png");  //定义文件的类型

function random($len) {

    $srcstr="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

         $strs= "";

         for($i= 0; $i < $len; $i ++) {

                   $strs.= $srcstr [mt_rand ( 0, 60 )]; //mt_rand生成更好的随机数

         }

         //      return strtoupper ( $strs );             //将字符串转化为大写字母

         return  $strs ;

}

$str = random ( 4 );

$width = 120;

$height = 35;

$image = imagecreate ( $width, $height );                        //创建图像

$back = imagecolorallocate ( $image, 0xFF,0xFF, 0xFF );        //设置背景颜色

$font = imagecolorallocate ( $image, 287,330, 347 );             //绘制模糊作用点

$font_color = imagecolorallocate ( $image,

mt_rand ( 50, 120 ), mt_rand ( 50, 120 ), mt_rand ( 50, 120 ) ); //设置字体颜色

for($i = 0; $i < 500; $i ++) {

         //画一个单一像素

         imagesetpixel( $image, mt_rand ( 0, $width ), mt_rand ( 0, $height ), imagecolorallocate ($image, mt_rand ( 1, 255 ), mt_rand ( 1, 255 ), mt_rand ( 1, 255 ) ) );

}

$font_file =$_SERVER['DOCUMENT_ROOT'].'arial.ttf'; //获取字体的文件位置

//设置字体的位置和颜色

imagettftext($image, mt_rand(15,28),mt_rand(-30,30), 5, 30, $font_color, $font_file, substr($str,0,1));

imagettftext($image, mt_rand(15,28),mt_rand(-30,30), 35, 30, $font_color, $font_file, substr($str,1,1));

imagettftext($image, mt_rand(15,28),mt_rand(-30,30), 65, 30, $font_color, $font_file, substr($str,2,1));

imagettftext($image, mt_rand(15,28),mt_rand(-30,30), 95, 30, $font_color, $font_file, substr($str,3,1));

imagerectangle($image, 0, 0, $width-1,

$height-1, $font); //画一个矩形

imagepng($image);                 //显示

imagedestroy($image);          //结束

?>

功能截图:

image
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PHP 学习目录 ├─PHP视频教程 1 LAMP网站构建 │ ├─PHP教程 1.1.1 新版视频形式介绍│ ...
    曹渊说创业阅读 16,175评论 29 417
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,811评论 25 708
  • 花开花谢又一年 青春的你 往事岂止一念 我 不断的回首 伫足 然而 时光 却扔下你 轰轰烈烈的向前奔去 为了不负...
    比秋乡阅读 125评论 2 4
  • 我不知道明天会是怎样,我却在今天知道原来一直崇拜的父亲,在外面并不是那个顶天立地的男人,而是一个基本一事无成还受人...
    落桐梓阅读 106评论 0 0
  • 黑与白,透镜中品味不一样的韵味,过滤纷乱复杂的干扰,回归简单。摄影如此,生活亦如此。
    牧长歌阅读 273评论 0 0