/system/heleprs/captcha_helper.php CI验证码

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter CAPTCHA Helper
 * CI 验证码
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/helpers/xml_helper.html
 */

// ------------------------------------------------------------------------

/**
 * Create CAPTCHA
 * 创建验证码
 * 
 * @access	public
 * @param	array	array of data for the CAPTCHA   数组中的数据验证
 * @param	string	path to create the image in     路径来创建图像
 * @param	string	URL to the CAPTCHA image folder CAPTCHA图像文件夹的URL
 * @param	string	server path to font             服务器字体路径
 * @return	string
 */
if ( ! function_exists('create_captcha'))
{
	function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
	{
		$defaults = array(
					'word' => '',         //关键字
					'img_path' => '',     //图片保存路径
					'img_url' => '',      //图片访问Url
					'img_width' => '150', //图片宽度
					'img_height' => '30', //高度
					'font_path' => '',    //文字路径
					'expiration' => 7200); //到期时间

		//开始循环
		//
		foreach ($defaults as $key => $val)
		{
			//如果$data不为数组
			if ( ! is_array($data))
			{
				//判断$word这种变量是否存在,如果不存在或者为空,初始化一下这些变量
				if ( ! isset($$key) OR $$key == '')
				{
					$$key = $val;
				}
			}
			else
			{
				//设置取得一个$data里面相应的值,
				$$key = ( ! isset($data[$key])) ? $val : $data[$key];
			}
		}

		//如果图片保存路径或者图片url为空,返回false
		if ($img_path == '' OR $img_url == '')
		{
			return FALSE;
		}

		//如果不为目录,返回空
		if ( ! @is_dir($img_path))
		{
			return FALSE;
		}

		//如果不可写返回为空
		if ( ! is_writable($img_path))
		{
			return FALSE;
		}
        //php 5.0.0 extension_loaded() 使用内置扩展名称来测试一个扩展是否有效
        
		if ( ! extension_loaded('gd'))
		{
			return FALSE;
		}

		// -----------------------------------
		// Remove old images 删除老的图片
		// -----------------------------------

	
		list($usec, $sec) = explode(" ", microtime());
		$now = ((float)$usec + (float)$sec); //由时间截和微秒组加起来的$now值

		//打开当前的目录 opendir($img_path)
		$current_dir = @opendir($img_path);

		//循环开始读取所有文件 readdir($current_dir)
		while ($filename = @readdir($current_dir))
		{
			//如果不为. 或者.. 或者index.html
			if ($filename != "." and $filename != ".." and $filename != "index.html")
			{
				//将文件名的.jpg替换为空值
				$name = str_replace(".jpg", "", $filename);

				//如果文件名加上过段时间还要小于当前时间的话,直接删除该文件
				if (($name + $expiration) < $now)
				{
					@unlink($img_path.$filename);
				}
			}
		}

		@closedir($current_dir); //关闭opendir();

		// -----------------------------------
		// Do we have a "word" yet?
		// 我们有一个“字”了吗?
		// -----------------------------------

	   if ($word == '')
	   {
	   	    //需要生成的随机数
			$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

			//
			$str = '';
			//长度为8
			for ($i = 0; $i < 8; $i++)
			{
				
				$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
			}

			$word = $str;
	   }

		// -----------------------------------
		// Determine angle and position
		// 确定角度和位置
		// -----------------------------------

		$length	= strlen($word); //字符串的长度
		//是要设置解度
		//如果长度大于6
		//从长度大于6的负数开始到长度减去6的正数中生成一个随机数
		$angle	= ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
		//x的开始 从6到 360/$length-16之间生成随机数
		$x_axis	= rand(6, (360/$length)-16);
		//y
		//如果$angle的值大于0的值
		//生成 高与宽之间的随机数
		//否则生成6与图片高度的随机数
		$y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);

		// -----------------------------------
		// Create image
		// 创建图片
		// -----------------------------------

		// PHP.net recommends imagecreatetruecolor(), but it isn't always available
		// PHP.net建议imagecreatetruecolor(),但它并不总是可用的
		// 判断imagecreatetruecolor 函数是否可用
		if (function_exists('imagecreatetruecolor'))
		{
			$im = imagecreatetruecolor($img_width, $img_height);
		}
		else
		{
			//如果不行用imagecreate进行
			$im = imagecreate($img_width, $img_height);
		}

		// -----------------------------------
		//  Assign colors
		// -----------------------------------
        
		//背景颜色为白色
		//imagecolorallocate()
		$bg_color		= imagecolorallocate ($im, 255, 255, 255);
		//表框颜色 
		$border_color	= imagecolorallocate ($im, 153, 102, 102);
		//文字颜色
		$text_color		= imagecolorallocate ($im, 204, 153, 153);
		//格子的颜色
		$grid_color		= imagecolorallocate($im, 255, 182, 182);
		//阴影的颜色
		$shadow_color	= imagecolorallocate($im, 255, 240, 240);

		// -----------------------------------
		//  Create the rectangle
		//  创建矩形
		// -----------------------------------

		
        /* bool imagefilledrectangle(resourec $image, int $x1, int $y1, int $x2, int $y2, int $color);
         * imagefilledrectangle() 在image图片中画一个用color颜色填充了的矩形,其左上角坐标为x1, y1,右下角坐标为x2, y2
         * 0,0,是图像的最左上角
         * */
		ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);

		// -----------------------------------
		//  Create the spiral pattern
		//  创建螺旋图案
		// -----------------------------------

		$theta		= 1;
		$thetac		= 7;
		$radius		= 16;  //半径
		$circles	= 20;  //圆
		$points		= 32;  //点

		//开始循环圆减以点的积,然后++
		for ($i = 0; $i < ($circles * $points) - 1; $i++)
		{
			
			$theta = $theta + $thetac; //8
			
			//用半径(16)*当点的面的点除以点(32)
			$rad = $radius * ($i / $points );
			
			//cos cos() 返回参数 arg 的余弦值。参数 arg 的单位为弧度。
			$x = ($rad * cos($theta)) + $x_axis;
			//sin() sin() 返回参数 arg 的正弦值。参数 arg 的单位为弧度。
			$y = ($rad * sin($theta)) + $y_axis;
			
			$theta = $theta + $thetac;
			
			$rad1 = $radius * (($i + 1) / $points);
			
			$x1 = ($rad1 * cos($theta)) + $x_axis;
			
			$y1 = ($rad1 * sin($theta )) + $y_axis;
			
			//imageline — 画一条线段
			imageline($im, $x, $y, $x1, $y1, $grid_color);
			
			$theta = $theta - $thetac;
		}

		// -----------------------------------
		//  Write the text
		//  写入文本
		// -----------------------------------

		//判断字体是否存在,并且imagettftext函数是否存在
		$use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;

		//如果字体不存在
		if ($use_font == FALSE)
		{
			$font_size = 5; //字体大小
			$x = rand(0, $img_width/($length/3)); //x: 
			$y = 0;
		}
		else
		{
			$font_size	= 16;
			$x = rand(0, $img_width/($length/1.5));
			$y = $font_size+2;
		}

		//开始循环文字
		for ($i = 0; $i < strlen($word); $i++)
		{
			//没有字
			if ($use_font == FALSE)
			{
				$y = rand(0 , $img_height/2);
				imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
				$x += ($font_size*2);
			}
			else
			{
				//有字体
				$y = rand($img_height/2, $img_height-3);
				imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
				$x += $font_size;
			}
		}


		// -----------------------------------
		//  Create the border
		//  创建边框 imagerectangle()
		// -----------------------------------

		imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);

		// -----------------------------------
		//  Generate the image 生成图像
		// -----------------------------------

		$img_name = $now.'.jpg'; //图片

		ImageJPEG($im, $img_path.$img_name); //生成图片

		//图片html
		$img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";

		//消毁图像资源
		ImageDestroy($im);

		//返回数组
		return array('word' => $word, 'time' => $now, 'image' => $img);
	}
}

// ------------------------------------------------------------------------
// captcha
/* End of file captcha_helper.php */
/* Location: ./system/heleprs/captcha_helper.php */

  

posted @ 2013-06-07 10:41  简单--生活  阅读(510)  评论(0编辑  收藏  举报
简单--生活(CSDN)