php图像码验证,图像验证码 PHP

从开源代码中摘录出的图像验证码类

class es_image

{

/**

+----------------------------------------------------------

* 根据给定的字符串生成图像

+----------------------------------------------------------

* @static

* @access public

+----------------------------------------------------------

* @param string $string 字符串

* @param string $size 图像大小 width,height 或者 array(width,height)

* @param string $font 字体信息 fontface,fontsize 或者 array(fontface,fontsize)

* @param string $type 图像格式 默认PNG

* @param integer $disturb 是否干扰 1 点干扰 2 线干扰 3 复合干扰 0 无干扰

* @param bool $border 是否加边框 array(color)

+----------------------------------------------------------

* @return string

+----------------------------------------------------------

*/

static function buildString($string, $rgb = array(), $filename = '', $type = 'png', $disturb = 3, $border = true,$font,$size)

{

if (is_string($size)) {

$size = explode(',', $size);

}

$width = $size[0];

$height = $size[1];

if (is_string($font)) {

$font = explode(',', $font);

}

$fontface = $font[0];

$fontsize = $font[1];

$length = strlen($string);

$width = 100;

$height = 30;

if ($type != 'gif' && function_exists('imagecreatetruecolor')) {

$im = @imagecreatetruecolor($width, $height);

} else {

$im = @imagecreate($width, $height);

}

if (empty($rgb)) {

$color = imagecolorallocate($im, 102, 104, 104);

} else {

$color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);

}

$backColor = imagecolorallocate($im, 255, 255, 255);

//背景色(随机)

$borderColor = imagecolorallocate($im, 100, 100, 100);

//边框色

$pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

//点颜色

@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);

@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);

$size = 18;

$font = 'c:/windows/fonts/arial.ttf';

imagettftext($im, $size, 0, 23, 24, $color, $font, $string);

if (!empty($disturb)) {

// 添加干扰

if ($disturb == 1) {

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

imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);

}

} elseif ($disturb == 2) {

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

imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $pointColor);

}

} else {

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

imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $pointColor);

}

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

imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);

}

}

}

es_image::output($im, $type, $filename);

}

static function output($im, $type = 'gif', $filename = '')

{

ob_clean();

header('Content-type: image/' . $type);

$ImageFun = 'image' . $type;

if (empty($filename)) {

if (!$ImageFun($im)) {

ob_clean();

header('Content-type: image/jpeg');

if (!imagejpeg($im)) {

ob_clean();

header('Content-type: image/png');

if (!imagepng($im)) {

es_session::delete('verify');

}

}

}

} else {

$ImageFun($im, $filename);

}

imagedestroy($im);

}

}

if (!function_exists('image_type_to_extension')) {

function image_type_to_extension($imagetype)

{

if (empty($imagetype)) {

return false;

}

switch ($imagetype) {

case IMAGETYPE_GIF:

return '.gif';

case IMAGETYPE_JPEG:

return '.jpeg';

case IMAGETYPE_PNG:

return '.png';

case IMAGETYPE_SWF:

return '.swf';

case IMAGETYPE_PSD:

return '.psd';

case IMAGETYPE_BMP:

return '.bmp';

case IMAGETYPE_TIFF_II:

return '.tiff';

case IMAGETYPE_TIFF_MM:

return '.tiff';

case IMAGETYPE_JPC:

return '.jpc';

case IMAGETYPE_JP2:

return '.jp2';

case IMAGETYPE_JPX:

return '.jpf';

case IMAGETYPE_JB2:

return '.jb2';

case IMAGETYPE_SWC:

return '.swc';

case IMAGETYPE_IFF:

return '.aiff';

case IMAGETYPE_WBMP:

return '.wbmp';

case IMAGETYPE_XBM:

return '.xbm';

default:

return false;

}

}

}

es_image::buildString('1234');

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值