Chinaunix首页 | 论坛 | 博客
  • 博客访问: 695103
  • 博文数量: 143
  • 博客积分: 1554
  • 博客等级: 上尉
  • 技术积分: 1767
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-10 11:50
文章分类

全部博文(143)

文章存档

2017年(2)

2016年(5)

2015年(16)

2014年(25)

2013年(27)

2012年(16)

2011年(52)

分类: PHP

2013-12-26 15:43:57


代码如下:

点击(此处)折叠或打开

  1. <?php
  2.     class Imagecode{
  3.         private $width ;
  4.         private $height;
  5.         private $counts;
  6.         private $distrubcode;
  7.         private $fonturl;
  8.         private $session;
  9.         function __construct($width = 120,$height = 30,$counts = 5,$distrubcode="1235467890qwertyuipkjhgfdaszxcvbnm",$fonturl="C:\Windows\Fonts\simfang.ttf"){
  10.             $this->width=$width;
  11.             $this->height=$height;
  12.             $this->counts=$counts;
  13.             $this->distrubcode=$distrubcode;
  14.             $this->fonturl=$fonturl;
  15.             $this->session=$this->sessioncode();
  16.             session_start();
  17.             $_SESSION['code']=$this->session;
  18.         }
  19.         
  20.          function imageout(){
  21.             $im=$this->createimagesource();
  22.             $this->setbackgroundcolor($im);
  23.             $this->set_code($im);
  24.             $this->setdistrubecode($im);
  25.             ImageGIF($im);
  26.             ImageDestroy($im);
  27.         }
  28.         
  29.         private function createimagesource(){
  30.             return imagecreate($this->width,$this->height);
  31.         }
  32.         private function setbackgroundcolor($im){
  33.             $bgcolor = ImageColorAllocate($im, rand(200,255),rand(200,255),rand(200,255));//±??°????
  34.             imagefill($im,0,0,$bgcolor);
  35.         }
  36.         private function setdistrubecode($im){
  37.             $count_h=$this->height;
  38.             $cou=floor($count_h*2);
  39.             for($i=0;$i<$cou;$i++){
  40.                 $x=rand(0,$this->width);
  41.                 $y=rand(0,$this->height);
  42.                 $jiaodu=rand(0,360);
  43.                 $fontsize=rand(8,15);
  44.                 $fonturl=$this->fonturl;
  45.                 $originalcode = $this->distrubcode;
  46.                 $countdistrub = strlen($originalcode);
  47.                 $dscode = $originalcode[rand(0,$countdistrub-1)];
  48.                 $color = ImageColorAllocate($im, rand(40,140),rand(40,140),rand(40,140));
  49.                 imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$dscode);
  50.                 
  51.             }
  52.         }
  53.         private function set_code($im){
  54.                 $width=$this->width;
  55.                 $counts=$this->counts;
  56.                 $height=$this->height;
  57.                 $scode=$this->session;
  58.                 $y=floor($height/2)+floor($height/4);
  59.                 $fontsize=rand(30,35);
  60.                 $fonturl="C:\Windows\Fonts\simfang.ttf";//$this->fonturl;
  61.                 
  62.                 $counts=$this->counts;
  63.                 for($i=0;$i<$counts;$i++){
  64.                     $char=$scode[$i];
  65.                     $x=floor($width/$counts)*$i+8;
  66.                     $jiaodu=rand(-20,30);
  67.                     $color = ImageColorAllocate($im,rand(0,50),rand(50,100),rand(100,140));
  68.                     imagettftext($im,$fontsize,$jiaodu,$x,$y,$color,$fonturl,$char);
  69.                 }
  70.                 
  71.             
  72.             
  73.         }
  74.         private function sessioncode(){
  75.                 $originalcode = $this->distrubcode;
  76.                 $countdistrub = strlen($originalcode);
  77.                 $_dscode = "";
  78.                 $counts=$this->counts;
  79.                 for($j=0;$j<$counts;$j++){
  80.                     $dscode = $originalcode[rand(0,$countdistrub-1)];
  81.                     $_dscode.=$dscode;
  82.                 }
  83.                 return $_dscode;
  84.                 
  85.         }
  86.     }
  87.     Header("Content-type: image/GIF");
  88.     $imagecode=new Imagecode(160,50);
  89.     $imagecode->imageout();


阅读(794) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~