欢迎来到福编程网,本站提供各种互联网专业知识!

PHP实现适用于自定义的验证码类

发布时间:2016-06-15 作者:投稿lijiao 来源:转载
这篇文章主要为大家详细介绍了PHP实现适用于自定义的验证码类,使用对象编写的验证码类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了PHP验证码类,利用对象来实现的验证码类,供大家参考,具体内容如下

代码
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. Class Image{
  8. private $img;
  9. public $width = 85;
  10. public $height = 25;
  11. public $code;
  12. public $code_len = 4;
  13. public $code_str = "329832983DSDSKDSLKQWEWQ2lkfDSFSDjfdsfdsjwlkfj93290KFDSKJFDSOIDSLK";
  14. public $bg_color = '#DCDCDC';
  15. public $font_size = 16;
  16. public $font = 'font.ttf';
  17. public $font_color = '#000000';
  18. //创建验证码饿字符创
  19. public function create_code(){
  20. $code = '';
  21. for( $i=0;$i<$this->code_len;$i++ ){
  22. $code .= $this->code_str[mt_rand(0, strlen($this->code_str)-1)];
  23. }
  24. return $this->code = $code;
  25. }
  26. //输出图像
  27. public function getImage(){
  28. $w = $this->width;
  29. $h = $this->height;
  30. $bg_color = $this->bg_color;
  31. $img = imagecreatetruecolor($w, $h);
  32. $bg_color = imagecolorallocate($img,
  33. hexdec(substr($bg_color, 1,2)), hexdec(substr($bg_color, 3,2)), hexdec(substr($bg_color, 5,2)));
  34. imagefill($img, 0, 0, $bg_color);
  35. $this->img = $img;
  36. $this->create_font();
  37. $this->create_pix();
  38. $this->show_code();
  39. }
  40. //写入验证码
  41. public function create_font(){
  42. $this->create_code();
  43. $color = $this->font_color;
  44. $font_color = imagecolorallocate($this->img, hexdec(substr($color,1,2)), hexdec(substr($color, 3,2)), hexdec(substr($color,5,2)));
  45. $x = $this->width/$this->code_len;
  46. for( $i=0;$i<$this->code_len;$i++ ){
  47. $txt_color = imagecolorallocate($this->img, mt_rand(0,100), mt_rand(0, 150), mt_rand(0, 200));
  48. imagettftext($this->img, $this->font_size, mt_rand(-30, 30), $x*$i+mt_rand(3, 6), mt_rand($this->height/1.2, $this->height), $txt_color, $this->font , $this->code[$i]);
  49. //imagestring($this->img, $this->font_size, $x*$i+mt_rand(3, 6),mt_rand(0, $this->height/4) , $this->code[$i], $font_color);
  50. }
  51. $this->font_color = $font_color;
  52. }
  53. //画干扰线
  54. public function create_pix(){
  55. $pix_color= $this->font_color;
  56. for($i=0;$i<100;$i++){
  57. imagesetpixel($this->img, mt_rand(0, $this->width),mt_rand(0, $this->height), $pix_color);
  58. }
  59. for($j=0;$j<4;$j++){
  60. imagesetthickness($this->img, mt_rand(1, 2));
  61. imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
  62. }
  63. }
  64. //得到验证码
  65. public function getCode(){
  66. return strtoupper($this->code);
  67. }
  68. //输出验证码
  69. private function show_code(){
  70. header("Content-type:image/png");
  71. imagepng($this->img);
  72. imagedestroy($this->img);
  73. }
  74. }

效果图:

精彩专题分享:ASP.NET验证码大全 PHP验证码大全 java验证码大全

以上就是使用对象编写的验证码类的全部内容,希望对大家学习PHP程序设计有所帮助。

相关推荐

返回顶部