大氣漂亮的驗證碼

來源:互聯網
上載者:User
自己匯入字型,可以按照自己的額需要隨便修改。
字型建議是用粗的
  1. class Imagecode{
  2. private $width ;
  3. private $height;
  4. private $counts;
  5. private $distrubcode;
  6. private $fonturl;
  7. private $fonturlbg;
  8. private $session;
  9. /**
  10. * Enter description here...
  11. *
  12. * @param unknown_type $width 寬度
  13. * @param unknown_type $height 高度
  14. * @param unknown_type $counts 字元個數
  15. * @param unknown_type $distrubcode 隨機字元範圍
  16. * @param unknown_type $fonturl 嚴重碼字型
  17. * @param unknown_type $fonturlbg 幹擾字元字型
  18. */
  19. function __construct($width = 120,$height = 30,$counts = 5,$distrubcode="1235467890qwertyuipkjhgfdaszxcvbnm",$fonturl="msyhbd.ttf",$fonturlbg='f.ttf'){
  20. $this->width=$width;
  21. $this->height=$height;
  22. $this->counts=$counts;
  23. $this->distrubcode=$distrubcode;
  24. $this->fonturl=$fonturl;
  25. $this->fonturlbg=$fonturlbg ? $fonturlbg :$fonturl;//未設定幹擾字型時使用
  26. $this->session=$this->sessioncode();
  27. session_start();
  28. $_SESSION['code']=$this->session;
  29. }
  30. function imageout(){
  31. $im=$this->createimagesource();
  32. $this->setbackgroundcolor($im);
  33. $this->set_code($im);
  34. $this->setdistrubecode($im);
  35. ImageGIF($im);
  36. ImageDestroy($im);
  37. }
  38. private function createimagesource(){
  39. return imagecreate($this->width,$this->height);
  40. }
  41. private function setbackgroundcolor($im){
  42. $bgcolor = ImageColorAllocate($im, rand(200,255),rand(200,255),rand(200,255));//±3?°??é?
  43. imagefill($im,0,0,$bgcolor);
  44. }
  45. private function setdistrubecode($im){
  46. $count_h=$this->height;
  47. $cou=floor($count_h*2);
  48. for($i=0;$i<$cou;$i++){
  49. $x=rand(0,$this->width);
  50. $y=rand(0,$this->height);
  51. $jiaodu=rand(0,360);
  52. $fontsize=rand(8,15);
  53. $fonturl=$this->fonturlbg;
  54. $originalcode = $this->distrubcode;
  55. $countdistrub = strlen($originalcode);
  56. $dscode = $originalcode[rand(0,$countdistrub-1)];
  57. $color = ImageColorAllocate($im, rand(40,140),rand(40,140),rand(40,140));//幹擾文字顏色
  58. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$dscode);
  59. }
  60. }
  61. private function set_code($im){
  62. $width=$this->width;
  63. $counts=$this->counts;
  64. $height=$this->height;
  65. $scode=$this->session;
  66. $y=floor($height/2)+floor($height/4);
  67. $fontsize=rand(30,35);
  68. $fonturl=$this->fonturl;
  69. $counts=$this->counts;
  70. for($i=0;$i<$counts;$i++){
  71. $char=$scode[$i];
  72. $x=floor($width/$counts)*$i+8;
  73. $jiaodu=rand(-20,30);
  74. $color = ImageColorAllocate($im,rand(0,200),rand(50,200),rand(100,240));//文字顏色
  75. imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$char);
  76. }
  77. }
  78. private function sessioncode(){
  79. $originalcode = $this->distrubcode;
  80. $countdistrub = strlen($originalcode);
  81. $_dscode = "";
  82. $counts=$this->counts;
  83. for($j=0;$j<$counts;$j++){
  84. $dscode = $originalcode[rand(0,$countdistrub-1)];
  85. $_dscode.=$dscode;
  86. }
  87. return $_dscode;
  88. }
  89. }
  90. Header("Content-type: image/GIF");
  91. $imagecode=new Imagecode(160,50);
  92. $imagecode->imageout();
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.