这是一个用php生成gif动画来实现动态图片验证码的程序,非常实用,是php用户注册登录界面经常需要用到的验证码程序,有需要gif动态图片验证码功能的朋友可以收藏一下。
第一步:创建imagecode.php文件,验证码生成函数代码如下:
<?php/** * imagecode 生成gif图片验证 * @param $string 字符串 * @param $width 宽度 * @param $height 高度 * */function imagecode($string = '', $width = 75, $height = 25){ $authstr = $string ? $string : ((time() % 2 == 0) ? mt_rand(1000, 9999) : mt_rand(10000, 99999)); $board_width = $width; $board_height = $height; // 生成一个32帧的gif动画 for($i = 0; $i 0) // 屏蔽第一帧 { for ($k = 0; $k source is not a gif image!, 'err02' =>unintelligible flag , 'err03' =>could not make animation from animated gif source, ); 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' ) { if (!is_array($gif_src) && !is_array($gif_tim)) { printf ( %s: %s, $this->ver, $this->err['err00']); exit( 0 ); } $this->lop = ($gif_lop > -1) ? $gif_lop : 0; $this->dis = ($gif_dis > -1) ? (( $gif_dis col = ($gif_red > -1 && $gif_grn > -1 && $gif_blu > -1) ? ($gif_red | ($gif_grn << 8) | ($gif_blu ver, $this->err [ 'err02' ], $gif_mod); exit(0); } if (substr($this->buf[$i], 0, 6) != gif87a && substr($this->buf [$i], 0, 6) != gif89a) { printf( %s: %d %s, $this->ver, $i, $this->err ['err01']); exit(0); } for ($j = (13 + 3 * (2 buf [$i]{$j}) { case !: if ((substr($this->buf[$i], ($j + 3), 8)) == netscape) { printf( %s: %s ( %s source )!, $this->ver, $this->err ['err03'], ($i + 1)); exit( 0 ); } break; case ;: $k = false; break; } } } gifencoder::gifaddheader(); for($i = 0, $count_buf = count($this->buf); $i buf[0]{10}) & 0x80 ) { $cmap = 3 * ( 2 gif .= substr ( $this->buf [ 0 ], 6, 7); $this->gif .= substr ( $this->buf [ 0 ], 13, $cmap); $this->gif .= !\377\13netscape2.0\3\1 . gifencoder::gifword ( $this->lop ) . \0; } } function gifaddframes ( $i, $d ) { $locals_str = 13 + 3 * (2 buf[$i]) - $locals_str - 1; $locals_tmp = substr ($this->buf[$i], $locals_str, $locals_end); $global_len = 2 > 8) & 0xff) . \x0\x0; if ( $this->col > -1 && ord($this->buf[$i]{10}) & 0x80) { for($j = 0; $j > 0) & 0xff && ord ( $locals_rgb { 3 * $j + 1 } ) == ( $this->col >> 8 ) & 0xff && ord ( $locals_rgb { 3 * $j + 2 } ) == ( $this->col >> 16 ) & 0xff ) { $locals_ext = !\xf9\x04 . chr(($this->dis 0) & 0xff) . chr (( $d >> 8) & 0xff) . chr ($j) . \x0; break; } } } switch ( $locals_tmp { 0 } ) { case !: $locals_img = substr($locals_tmp, 8, 10); $locals_tmp = substr($locals_tmp, 18, strlen ($locals_tmp) - 18); break; case ,: $locals_img = substr($locals_tmp, 0, 10); $locals_tmp = substr($locals_tmp, 10, strlen($locals_tmp) - 10); break; } if ( ord ( $this->buf[$i]{10} ) & 0x80 && $this->img > -1 ) { if ( $global_len == $locals_len ) { if ( gifencoder::gifblockcompare ( $global_rgb, $locals_rgb, $global_len ) ) { $this->gif .= ( $locals_ext . $locals_img . $locals_tmp ); } else { $byte = ord ( $locals_img{9}); $byte |= 0x80; $byte &= 0xf8; $byte |= ( ord ( $this->buf [ 0 ]{10}) & 0x07); $locals_img{9} = chr($byte); $this->gif .= ($locals_ext . $locals_img . $locals_rgb . $locals_tmp); } } else { $byte = ord($locals_img{9}); $byte |= 0x80; $byte &= 0xf8; $byte |= (ord($this->buf[$i]{10}) & 0x07); $locals_img {9} = chr($byte); $this->gif .= ($locals_ext . $locals_img . $locals_rgb . $locals_tmp); } } else { $this->gif .= ( $locals_ext . $locals_img . $locals_tmp ); } $this->img = 1; } function gifaddfooter ( ) { $this->gif .= ;; } function gifblockcompare ( $globalblock, $localblock, $len ) { for ( $i = 0; $i > 8 ) & 0xff ) ); } function getanimation ( ) { return ($this->gif); } }
第二步:验证码生成,创建yzimg.php,并引用入文件imagecode.php。代码如下:
<?phpif(!isset($_session)){ session_start();}require_once(dirname(__file__).imagecode.php);unset($_session['yzm']);$randcode = '';$chars = 'abcdefghjkmnpqrstuvwxyzabcdefghjklmnprstuvwxyz23456789';for ( $i = 0; $i
第三步:调用和显示验证码,在需要添加验证码的页面加入以下代码,代码如下:
以上就是为大家分享的如何使用php生成动态验证码的相应代码,希望对大家的学习有所帮助。