1 <?php2 /**3 * 调用示例4 **/
5 session_start();6 $randCode = '';7 //验证码随机
8 $str="abcdefghjkmnpqrstuvwsyzABCDEFGHJKMNPQRSTUVWSYZ23456789";9 for($i=0;$i<4;$i++){10 $safe.=substr($str,rand(0,strlen($str)),1);11 }12 $_SESSION["imgcode"]=$safe;13 ImageCode($safe, 60); //显示GIF动画
14
15 /**16 * ImageCode 生成GIF图片验证17 * @param $string 字符串18 * @param $width 宽度19 * @param $height 高度20 **/
21 function ImageCode($string = '', $width = 75, $height = 25){22 $authstr = $string ? $string : ((time() % 2 == 0) ? mt_rand(1000, 9999) : mt_rand(10000, 99999));23 $board_width = $width;24 $board_height = $height;25 //生成一个32帧的GIF动画
26 for($i = 0; $i < 32; $i++){27 ob_start();28 $image = imagecreate($board_width, $board_height);29 imagecolorallocate($image, 0,0,0);30 //设定文字颜色数组
31 $colorList[] = ImageColorAllocate($image, 15,73,210);32 $colorList[] = ImageColorAllocate($image, 0,64,0);33 $colorList[] = ImageColorAllocate($image, 0,0,64);34 $colorList[] = ImageColorAllocate($image, 0,128,128);35 $colorList[] = ImageColorAllocate($image, 27,52,47);36 $colorList[] = ImageColorAllocate($image, 51,0,102);37 $colorList[] = ImageColorAllocate($image, 0,0,145);38 $colorList[] = ImageColorAllocate($image, 0,0,113);39 $colorList[] = ImageColorAllocate($image, 0,51,51);40 $colorList[] = ImageColorAllocate($image, 158,180,35);41 $colorList[] = ImageColorAllocate($image, 59,59,59);42 $colorList[] = ImageColorAllocate($image, 0,0,0);43 $colorList[] = ImageColorAllocate($image, 1,128,180);44 $colorList[] = ImageColorAllocate($image, 0,153,51);45 $colorList[] = ImageColorAllocate($image, 60,131,1);46 $colorList[] = ImageColorAllocate($image, 0,0,0);47 $fontcolor = ImageColorAllocate($image, 0,0,0);48 $gray = ImageColorAllocate($image, 245,245,245);49 $color = imagecolorallocate($image, 255,255,255);50 $color2 = imagecolorallocate($image, 255,0,0);51 imagefill($image, 0, 0, $gray);52 $space = 15;//字符间距
53 if($i > 0){ //屏蔽第一帧
54 for ($k = 0; $k < strlen($authstr); $k++) {55 $colorRandom = mt_rand(0,sizeof($colorList)-1);56 $float_top = rand(0,4);57 $float_left = rand(0,3);58 imagestring($image, 6, $space * $k, $top + $float_top, substr($authstr, $k, 1), $colorList[$colorRandom]);59 }60 }61 for ($k = 0; $k < 20; $k++) {62 $colorRandom = mt_rand(0,sizeof($colorList)-1);63 imagesetpixel($image, rand()%70 , rand()%15 , $colorList[$colorRandom]);64
65 }66 //添加干扰线
67 for($k = 0; $k < 5; $k++){68 $colorRandom = mt_rand(0, sizeof($colorList)-1);69 //$todrawline = rand(0,1);
70 $todrawline = 1;71 if($todrawline){72 imageline($image, mt_rand(0, $board_width), mt_rand(0,$board_height), mt_rand(0,$board_width), mt_rand(0,$board_height), $colorList[$colorRandom]);73 }else{74 $w = mt_rand(0,$board_width);75 $h = mt_rand(0,$board_width);76 imagearc($image, $board_width - floor($w / 2) , floor($h / 2), $w, $h, rand(90,180), rand(180,270), $colorList[$colorRandom]);77 }78 }79 imagegif($image);80 imagedestroy($image);81 $imagedata[] = ob_get_contents();82 ob_clean();83 ++$i;84 }85 $gif = new GIFEncoder($imagedata);86 Header ('Content-type:image/gif');87 echo $gif->GetAnimation();88 }89
90
91 /**92 * GIFEncoder类93 **/
94 classGIFEncoder {95 var $GIF = "GIF89a"; /*GIF header 6 bytes*/
96 var $VER = "GIFEncoder V2.06"; /*Encoder version*/
97 var $BUF = Array( );98 var $LOP = 0;99 var $DIS = 2;100 var $COL = -1;101 var $IMG = -1;102 var $ERR = Array(103 'ERR00' =>"Does not supported function for only one image!",
104 'ERR01' =>"Source is not a GIF image!",
105 'ERR02' =>"Unintelligible flag ",
106 'ERR03' =>"Could not make animation from animated GIF source",
107 );108 function GIFEncoder ($GIF_src, $GIF_dly = 100, $GIF_lop = 0, $GIF_dis = 0, $GIF_red = 0, $GIF_grn = 0, $GIF_blu = 0, $GIF_mod = 'bin') {109 if (!is_array($GIF_src) && !is_array($GIF_tim)) {110 printf ( "%s: %s", $this->VER, $this->ERR['ERR00']);111 exit( 0);112 }113 $this->LOP = ($GIF_lop > -1) ? $GIF_lop : 0;114 $this->DIS = ($GIF_dis > -1) ? (( $GIF_dis < 3 ) ? $GIF_dis : 3) : 2;115 $this->COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ? ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;116
117 for ($i = 0, $src_count = count($GIF_src); $i < $src_count; $i++) {118 if (strToLower( $GIF_mod ) == "url") {119 $this->BUF[] = fread (fopen($GIF_src [$i], "rb"), filesize ($GIF_src [$i]));120 }elseif(strToLower($GIF_mod) == "bin") {121 $this->BUF [ ] = $GIF_src [ $i];122 }else{123 printf("%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod);124 exit(0);125 }126 if (substr($this->BUF[$i], 0, 6) != "GIF87a" && substr($this->BUF [$i], 0, 6) != "GIF89a") {127 printf( "%s: %d %s", $this->VER, $i, $this->ERR ['ERR01']);128 exit(0);129 }130 for ($j = (13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07 ))), $k = TRUE; $k; $j++){131 switch ($this->BUF [$i]{$j}) {132 case "!":
133 if ((substr($this->BUF[$i], ($j + 3), 8)) == "NETSCAPE") {134 printf( "%s: %s ( %s source )!", $this->VER, $this->ERR ['ERR03'], ($i + 1));135 exit( 0);136 }137 break;138 case ";":
139 $k = FALSE;140 break;141 }142 }143 }144 GIFEncoder::GIFAddHeader();145 for($i = 0, $count_buf = count($this->BUF); $i < $count_buf; $i++) {146 GIFEncoder::GIFAddFrames($i, $GIF_dly[$i]);147 }148 GIFEncoder::GIFAddFooter();149 }150
151 functionGIFAddHeader ( ) {152 $cmap = 0;153 if (ord($this->BUF[0]{10}) & 0x80) {154 $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ]{10} ) & 0x07));155
156 $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7);157 $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap);158 $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord ( $this->LOP ) . "\0";159 }160 }161
162 function GIFAddFrames ( $i, $d) {163 $Locals_str = 13 + 3 * (2 <BUF[$i]{10}) & 0x07));164 $Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1;165 $Locals_tmp = substr ($this->BUF[$i], $Locals_str, $Locals_end);166 $Global_len = 2 << (ord( $this->BUF [0]{10} ) & 0x07);167 $Locals_len = 2 << (ord( $this->BUF[$i]{10}) & 0x07);168 $Global_rgb = substr($this->BUF[0], 13, 3 * (2 << ( ord ( $this->BUF[0]{10} ) & 0x07)));169 $Locals_rgb = substr ( $this->BUF[$i], 13, 3 * (2 << ( ord ( $this->BUF[$i]{10} ) & 0x07)));170 $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 0) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";171 if ( $this->COL > -1 && ord($this->BUF[$i]{10}) & 0x80){172 for($j = 0; $j < (2 << (ord( $this->BUF[$i]{10}) & 0x07)); $j++) {173 if(ord ($Locals_rgb{3 * $j + 0}) == ($this->COL >> 0) & 0xFF && ord ( $Locals_rgb { 3 * $j + 1 } ) == ( $this->COL >> 8 ) & 0xFF && ord ( $Locals_rgb { 3 * $j + 2 } ) == ( $this->COL >> 16 ) & 0xFF) {174 $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 1) . chr (( $d >> 0) & 0xFF) . chr (( $d >> 8) & 0xFF) . chr ($j) . "\x0";175 break;176 }177 }178 }179 switch ( $Locals_tmp { 0} ) {180 case "!":
181 $Locals_img = substr($Locals_tmp, 8, 10);182 $Locals_tmp = substr($Locals_tmp, 18, strlen ($Locals_tmp) - 18);183 break;184 case ",":
185 $Locals_img = substr($Locals_tmp, 0, 10);186 $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);187 break;188 }189 if ( ord ( $this->BUF[$i]{10} ) & 0x80 && $this->IMG > -1) {190 if ( $Global_len == $Locals_len) {191 if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len) ) {192 $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp);193 }else{194 $byte = ord ( $Locals_img{9});195 $byte |= 0x80;196 $byte &= 0xF8;197 $byte |= ( ord ( $this->BUF [ 0 ]{10}) & 0x07);198 $Locals_img{9} = chr($byte);199 $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);200 }201 }else{202 $byte = ord($Locals_img{9});203 $byte |= 0x80;204 $byte &= 0xF8;205 $byte |= (ord($this->BUF[$i]{10}) & 0x07);206 $Locals_img {9} = chr($byte);207 $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);208 }209 }else{210 $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp);211 }212 $this->IMG = 1;213 }214
215 functionGIFAddFooter ( ) {216 $this->GIF .= ";";217 }218
219 function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len) {220 for ( $i = 0; $i < $Len; $i++) {221 if($GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } || $GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } || $GlobalBlock { 3 * $i + 2 } != $LocalBlock{3 * $i + 2}) {222 return ( 0);223 }224 }225 return ( 1);226 }227
228 function GIFWord ( $int) {229 return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF) );230 }231
232 functionGetAnimation ( ) {233 return ($this->GIF);234 }235 }