码迷,mamicode.com
首页 > Web开发 > 详细

php验证码

时间:2015-12-30 22:07:40      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

<?php

/*
 * php中验证码的实现
 */

//创建画布
$width=500;
$height=200;

//创建一个真彩色画布
//$img=imagecreatetruecolor($width, $height);

//分配颜色
//$green=imagecolorallocate($img, 0, 255, 0);

//填充画布
//imagefill($img, 0, 0, $green);

//基于图片创建背景图
$bg_file=‘./bg‘.mt_rand(1,3).‘.jpg‘;

$img=imagecreatefromjpeg($bg_file);


//验证码的值
$chars=‘ABCDEFGHI1234567890‘;
$chars_len=strlen($chars);
$code_len=4;//码值的长度
$code=‘‘;//初始码值的字符串
for($i=0;$i<$code_len;$i++)
{
    $rand_index=mt_rand(0, $chars_len-1);
    $code.=$chars[$rand_index];
}
//随机分配字符串颜色
$str_color=mt_rand(1, 2)==1?imagecolorallocate($img, 0, 0, 0):imagecolorallocate($img, 255, 255, 255);

//字符串
$font=5;
$x=55;
$y=2;

//居中的方式
$img_w=imagesx($img);
$img_h=imagesy($img);
//字体尺寸
$font_w=imagefontwidth($font);
$font_y=imagefontheight($font);
//字符串宽度
$code_w=$font_w*$code_len;
$code_h=$font_y;

$x=($img_w-$code_w)/2;
$y=($img_h-$code_h)/2;
imagestring($img, $font, $x, $y, $code, $str_color);

//输出
header(‘Content-Type:image/jpeg‘);
imagejpeg($img);

//输出到文件  如果写第二个参数则输出到文件,不写则直接输出
//imagepng($img,‘./cc.png‘);
//header(‘Content-Type:image/png‘);
//imagepng($img);
?>

 

php验证码

标签:

原文地址:http://www.cnblogs.com/zywf/p/5090086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!