找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4785|回复: 0

[原]PHP 画图应用 验证码 柱状图

[复制链接]
发表于 2012-2-24 14:49:07 | 显示全部楼层 |阅读模式
Title:           PHP 画图应用 验证码 柱状图
Author:       MoreWindows
Blog:           http://blog.csdn.net/MoreWindows
KeyWord:   PHP画图 验证码 柱状图 imagefilledarc

阅读本文之前,推荐先参阅姊妹篇《PHP 画图基础》。
本篇介绍如何使用PHP常用的绘图函数来生成验证码图片和柱状图。
一.验证码
在网站中验证码是非常有用的,下图就是一个含4个数字的验证码图片。
简单的验证码图片主要通过在正确内容上增加一些干扰的点和线。这种方法实现起来方便容易,作为示范,本文实现了一个随机字体(有10种字体文件),支持随机文字颜色,有干扰点,干扰线的验证码类,此类可以批量在磁盘上生成验证码图片并指定验证码由多少个数字多少个字母组成。具体功能可以参阅代码:
  1. <?php
  2. //PHP生成验证码
  3. // by MoreWindows( http://blog.csdn.net/MoreWindows )
  4. class CSecurity_verify
  5. {
  6.         private $m_image;
  7.         private $m_dir_name;
  8.         private $m_image_width;
  9.         private $m_image_height;
  10.         private $m_digit_num;
  11.         private $m_letter_num;
  12.         private $m_font_color;
  13.         const NOISE_DOT_NUM = 100; //干扰点个数
  14.         const NOISE_LINE_NUM = 40; //干扰线个数
  15.        
  16.         /*
  17.          * $dir_name 保存验证码图片的文件夹目录(绝对路径)
  18.          * $digit_num 数字个数
  19.          * $letter_num 字母个数
  20.          * $width 验证码图片宽
  21.          * $height 验证码图片高
  22.         */
  23.         public function __construct($dir_name, $digit_num, $letter_num, $width = 140, $height = 40)
  24.         {
  25.                 $this->m_dir_name = $dir_name;
  26.                 $this->m_digit_num = $digit_num;
  27.                 $this->m_letter_num = $letter_num;
  28.                 $this->m_image_width = $width;
  29.                 $this->m_image_height = $height;
  30.         }
  31.        
  32.         /*
  33.          * 在指定目录上生成指定条件的验证码图片
  34.          * $verify_pic_num 要生成多少张验证码图片
  35.         */
  36.         public function BatchVerifyPicture($verify_pic_num)
  37.         {
  38.                 while ($verify_pic_num >= 0)
  39.                 {
  40.                         $verify_pic_num--;
  41.                         self::CreateVerifyImage();
  42.                         self::DrawNoiseDot();
  43.                         self::DrawNoiseLine();
  44.                         $verify_text = self::GetVerifyText();
  45.                         $filesize = self::DrawVerifyImage($verify_text);
  46.                         if ($filesize != -1)
  47.                                 echo $verify_text . ".png生成成功,大小" . $filesize . "字节 <br />";
  48.                         else
  49.                                 echo $verify_text . ".png生成失败<br />";
  50.                 }
  51.         }
  52.        
  53.         /*
  54.          * 创建图片
  55.         */
  56.         protected function CreateVerifyImage()
  57.         {
  58.                 $this->m_image = imagecreatetruecolor($this->m_image_width, $this->m_image_height) or die("CreateVerifyImage failde");
  59.                 $black_color = imagecolorallocate($this->m_image, 243, 251, 254);
  60.                 imagefill($this->m_image, 0, 0, $black_color);//设置底色
  61.                 //字体颜色
  62.                 $m_font_color = imagecolorallocate($this->m_image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
  63.         }       
  64.         /*
  65.          * 生成验证码内容
  66.          * 验证码中使用的字符,01IOQ容易混淆,故不用。
  67.         */
  68.         protected function GetVerifyText()
  69.         {
  70.                 $verify_text = "";
  71.                 $letter_array = "ABCDEFGHJKLMNPRSTUVWXYZ";
  72.                 $digit_num = $this->m_digit_num;
  73.                 $letter_num = $this->m_letter_num;
  74.                 while ($digit_num--) //数字
  75.                         $verify_text .= mt_rand(2, 9);
  76.                 while ($letter_num--) //字母
  77.                         $verify_text .= $letter_array[mt_rand(0, 22)];
  78.                 return $verify_text;
  79.         }
  80.        
  81.         /*
  82.          * 绘验证码
  83.         */
  84.         protected function DrawVerifyImage($verify_text)
  85.         {
  86.                 //字体文件
  87.                 $font_file = "ttfs\\t" . mt_rand(1, 10) . ".ttf";
  88.                 //
  89.                 $verify_text_show = "";
  90.                 for ($i = 0; $i < strlen($verify_text); $i++)
  91.                         $verify_text_show .= ($verify_text[$i] . " ");
  92.                 //文字的大小,角度,位置
  93.                 $font_size = 20;
  94.                 $font_angle = mt_rand(0, 5);
  95.                 $font_pos_x = 0;
  96.                 $font_pos_y = $this->m_image_height - 5;
  97.                 imagettftext($this->m_image, $font_size, $font_angle, $font_pos_x, $font_pos_y, $this->m_font_color, $font_file, $verify_text_show);
  98.                 $verify_image_filename = $this->m_dir_name . "\\$verify_text.png";
  99.                 if (!imagepng($this->m_image, $verify_image_filename))
  100.                         return -1;
  101.                 imagedestroy($this->m_image);
  102.                 return filesize($verify_image_filename);
  103.         }
  104.        
  105.         /*
  106.          * 绘干扰点
  107.         */
  108.         protected function DrawNoiseDot()
  109.         {
  110.                 $noise_dot_color = $this->m_font_color;
  111.                 for ($i = 0; $i < self::NOISE_DOT_NUM; $i++)
  112.                 {
  113.                         imagesetpixel($this->m_image, mt_rand(0, $this->m_image_width), mt_rand(0, $this->m_image_height), $noise_dot_color);
  114.                 }
  115.         }
  116.        
  117.         /*
  118.          * 绘干扰线
  119.         */       
  120.         protected function DrawNoiseLine()
  121.         {
  122.                 for ($i = 0; $i < self::NOISE_LINE_NUM; $i++)
  123.                 {
  124.                         $noise_line_color = imagecolorallocate($this->m_image, mt_rand(50, 120), mt_rand(50, 120), mt_rand(50, 120));
  125.                         imageline($this->m_image, mt_rand(0, $this->m_image_width), mt_rand(0, $this->m_image_height), mt_rand(0, $this->m_image_width), mt_rand(0, $this->m_image_height), $noise_line_color);
  126.                 }               
  127.         }
  128. }
  129. ?>
复制代码
再给出使用示例,运行后可以会D盘上生成6张验证码图片,代码如下:
  1. <?php
  2. require_once 'CSecurity_verify.php';
  3. $test = new CSecurity_verify("D:\", 4, 0);
  4. $test->BatchVerifyPicture(6);
  5. ?>
复制代码
生成的验证码效果如下所示:





当然还有很多特效可以加入的,如文字水波化、背景增加彩色小字母干扰等等,这些都可以有效的美化验证码图片。有需要的筒子们可以深入学习下,这里就不细究了。
注 程序所使用字体文件可以从C:\Windows\Fonts中选择,并拷贝到PHP文件所在目录中的ttfs文件夹。
二.柱状图
在PHP中绘制柱状图可以使用bool imagefilledarc( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color , int $style )函数。此函数的说明可以参考《PHP 画图基础》一文,柱状图原理很简单就是先用暗色绘制多层再用亮色绘制最上层,这样明暗对比就可以产生立体效果。具体过程可以参考下图:
再给出一个PHP根据各数据值来生成柱状图的示例代码:
  1. <?php
  2. //柱状图
  3. // by MoreWindows( http://blog.csdn.net/MoreWindows )
  4. $image_width = 400;
  5. $image_height = 300;
  6. $image = imagecreatetruecolor($image_width, $image_height);
  7. $black_color = imagecolorallocate($image, 243, 251, 254);
  8. imagefill($image, 0, 0, $black_color);//设置底色
  9. //亮色
  10. $gray_color     = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
  11. $navy_color     = imagecolorallocate($image, 0x00, 0x00, 0x80);
  12. $red_color      = imagecolorallocate($image, 0xFF, 0x00, 0x00);
  13. //暗色
  14. $darkgray_color = imagecolorallocate($image, 0x90, 0x90, 0x90);
  15. $darknavy_color = imagecolorallocate($image, 0x00, 0x00, 0x50);
  16. $darkred_color  = imagecolorallocate($image, 0x90, 0x00, 0x00);
  17. //各份份量大小
  18. $value_array = array(12.5, 8.4, 79.1);
  19. $all_value = array_sum($value_array);
  20. $color_array = array($gray_color, $navy_color, $red_color);
  21. $drak_color_array = array($darkgray_color, $darknavy_color, $darkred_color);
  22. //先用暗色绘制30层
  23. for ($i = 80; $i > 50; $i--)
  24. {
  25.         $angle_begin = 0;
  26.         $angle_end = 0;
  27.         foreach ($value_array as $j=>$val)
  28.         {       
  29.                 $angle_begin = $angle_end;
  30.                 $angle_end += $val * 360 / $all_value;
  31.                 imagefilledarc($image, 100, $i, 200, 100, $angle_begin, $angle_end, $drak_color_array[$j], IMG_ARC_PIE);
  32.         }
  33. }
  34. //最上层再用亮色绘图,这样就有立体效果了。
  35. $angle_begin = 0;
  36. $angle_end = 0;
  37. foreach ($value_array as $j=>$val)
  38. {       
  39.         $angle_begin = $angle_end;
  40.         $angle_end += $val * 360 / $all_value;
  41.         imagefilledarc($image, 100, $i, 200, 100, $angle_begin, $angle_end, $color_array[$j], IMG_ARC_PIE);
  42. }
  43. // flush image
  44. header('Content-type: image/png');
  45. imagepng($image);
  46. imagedestroy($image);
  47. ?>
复制代码
运行效果如下:
总体来说,PHP的绘图功能还是方便强大的,有需要的筒子们还可以试下PHPlot来绘图,其类库功能强大,使用也方便。


转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/7289686

作者:MoreWindows 发表于2012-2-24 10:24:48 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-30 00:33 , Processed in 0.012600 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表