Php将图像转换为具有ASCII艺术字符的HTML。

这个类实际应用价值不大,不过我觉得挺有意思的,所以拿出来跟大家分享一下。

原图:

转换后效果:

实际上就是把图片缩小以后,把每个像素点都用相应颜色的字母代替。使用时必须把原图缩小到很小的尺寸,不然字母太多屏幕都放不下。

example.php:

<?
include ('image2html.class.php');
?>

<html>
<head>
<style>
body{
	font-size: 8px;
	font-family: Courier; /* recommended use of terminal fonts like Courier, Lucida Console, etc */
	font-weight: bold;
}
</style>
</head>
<body>
<?
// parameter is the image that will be converted
$image			= new Image2HTML('girl.jpg');
// this chars will compose the art
$image->chars	= 'USETHISCHARS';
// place this where you want to show your art
$image->Show();
?>
</body>
</html>

类文件:

<?
class Image2HTML{

	var $image;
	var $width;
	var $height;
	// chars used to compose image
	var $chars	= 'B';

	function Image2HTML($image){
		$file_array		= explode('.',$image);
		$extension		= $file_array[sizeof($file_array)-1];

		switch ($extension){
			case 'jpg':
			case 'jpeg':
				$this->image	= imagecreatefromjpeg($image);
				break;
			case 'gif':
				$this->image	= imagecreatefromgif($image);
				break;
			case 'png':
				$this->image	= imagecreatefrompng($image);
				break;
		}
		$this->width	= imagesx($this->image);
		$this->height	= imagesy($this->image);
	}

	function PickColor($x, $y){
		$rgb = ImageColorAt($this->image, $x, $y);

		if(imageistruecolor($this->image)) {
			$r = strtoupper(dechex(($rgb >> 16) & 0xFF));
			$g = strtoupper(dechex(($rgb >> 8) & 0xFF));
			$b = strtoupper(dechex($rgb & 0xFF));
	    } else {
	        $i = imagecolorsforindex($this->image, $rgb);
	        $r = strtoupper(dechex($i['red']));
	        $g = strtoupper(dechex($i['green']));
	        $b = strtoupper(dechex($i['blue']));
	    }
		if (strlen($r) == 1){
			$r	= '0'.$r;
		}
		if (strlen($g) == 1){
			$g	= '0'.$g;
		}
		if (strlen($b) == 1){
			$b	= '0'.$b;
		}

		$rgb	= '#'.$r.$g.$b;

		return ($rgb);
	}

	function Show(){
		$print_font		= true;

		for ($i=0; $i<$this->height; $i++){
			for ($j=0; $j<$this->width; $j++){
				$color	= $this->PickColor($j,$i);
				if ($print_font)
					echo '<font color="'.$color.'">';
				echo $this->chars[rand(0,strlen($this->chars)-1)].$this->chars[rand(0,strlen($this->chars)-1)];
				$print_font	= false;
				if ($j	!= $this->width){
					$next_color	= $this->PickColor($j+1,$i);
					if ($color != $next_color){
						echo '</font>';
						$print_font	= true;
					}
				}
			}
			echo "</font><br>\n";
			flush();
			$print_font	= true;
		}
	}
}
?>

Php阻止机器自动群发垃圾信息的方法总结

最近网站被机器群发了很多信息,每天都要删几页信息很麻烦。最后对发布程序做了很多改进,目前效果不错,总结一下自己的方法:

1、启用图形验证码,这个其实一直都有,而且是中文验证码,可以屏蔽国外的群发。但最近好像对国内的不管用了。最近又增加了验证码判断reffer,这个必须的。

2、判断来源网址,发布提交页面判断来源网址,这个也是必须的。

3、每个输入表单都重命名并且加入了随机干扰字符,也就是每次提交的表单名称都是不一样的。这个短暂有效,但是后来发现又有很多群发信息。

4、每个表单前面的文字都进行了隐藏,在form后面使用Javascript脚本二次插入。这个改了以后,群发信息减少明显,基本没什么垃圾信息了。

5、在发布提交页面进行关键词过滤,只要提交内容包含和网站主题无关的关键词,发布状态自动设置为待审核状态。这个也是很有必要的,可以防止别人手工发布与网站主题无关的内容和非法内容。

6、对某些群发严重的IP进行屏蔽,可以判断IP所在地然后直接屏蔽掉整个地区,这个会影响某些正常用户发布,慎重使用!

7、今天又想到一个办法,在表单前面增加一个隐藏的文本框,文本框名称使用title或者content之类的很简单的名称,在提交页面判断这个文本框是否有输入,如果有输入就确定是Robot,直接阻止发布就可以了!2019年7月23日

经过以上设置,每天几页几页的垃圾信息没有了,效果很明显!:)

以上即是我用过的屏蔽机器群发信息的几个方法,个人觉得应该比较全面了。当然还有很多第三方的验证码效果也不错,不过我网站的用户群体互联网技能可能不是很高,怕影响用户体验所以没有采用。如果你有更好的防群发方法,欢迎在这里评论提出!

本文章为本站原创,如需转载请注明出处,谢谢合作!

Php结合Jquery实现在线裁切图片

Php结合Jquery实现可视编辑器,以便用户裁剪图像。它为用户界面生成HTML,JavaScript和CSS,让用户点击图像并定义要裁剪的图像区域。PHP类可以裁剪用户定义的图像,并将裁剪的图像保存到给定文件。

程序还是存在一些问题,比如裁剪框不能拖放改变大小,原图太大的话会超出屏幕等,有时间我会改进一下。

效果演示地址: http://www.songrao.com/demo/image-crop/exemple.php

源码下载地址:http://www.songrao.com/demo/image-crop-2010-09-03.zip

Php调整gif动画图片尺寸

此类可以调整GIF格式文件中的动画大小。它可以采用动画GIF图像文件,并将其帧文件提取到临时目录中。该类调整提取的图像的大小,并将动画重建为GIF格式的新文件。

用法示例

<?php 
require_once "gifresizer.php";	//Including our class
$gr = new gifresizer;	//New Instance Of GIFResizer
$gr->temp_dir = "frames"; //Used for extracting GIF Animation Frames
$gr->resize("gifs/1.gif","resized/1_resized.gif",160,90); //Resizing the animation into a new file.
?>
原图
缩略图
<? 
    /** 
    * Resizes Animated GIF Files
    *
    *	///IMPORTANT NOTE: The script needs a temporary directory where all the frames should be extracted. 
    *	Create a directory with a 777 permission level and write the path into $temp_dir variable below. 
    *	
    *	Default directory is "frames".
    */ 

    class gifresizer {

        public $temp_dir = "frames";
        private $pointer = 0;
        private $index = 0;
        private $globaldata = array();
        private $imagedata = array();
        private $imageinfo = array();
        private $handle = 0;
        private $orgvars = array();
        private $encdata = array();
        private $parsedfiles = array();
        private $originalwidth = 0;
        private $originalheight = 0;
        private $wr,$hr;
        private $props = array();
        private $decoding = false;

        /** 
        * Public part of the class
        * 
        * @orgfile - Original file path
        * @newfile - New filename with path
        * @width   - Desired image width 
        * @height  - Desired image height
        */ 
        function resize($orgfile,$newfile,$width,$height){
            $this->decode($orgfile);
            $this->wr=$width/$this->originalwidth;
            $this->hr=$height/$this->originalheight;
            $this->resizeframes();
            $this->encode($newfile,$width,$height);
            $this->clearframes();
        }	

        /** 
        * GIF Decoder function.
        * Parses the GIF animation into single frames.
        */
        private function decode($filename){
            $this->decoding = true;            
            $this->clearvariables();
            $this->loadfile($filename);
            $this->get_gif_header();
            $this->get_graphics_extension(0);
            $this->get_application_data();
            $this->get_application_data();
            $this->get_image_block(0);
            $this->get_graphics_extension(1);
            $this->get_comment_data();
            $this->get_application_data();
            $this->get_image_block(1);
            while(!$this->checkbyte(0x3b) && !$this->checkEOF()){
                $this->get_comment_data(1);
                $this->get_graphics_extension(2);
                $this->get_image_block(2);
            }
            $this->writeframes(time());		
            $this->closefile();
            $this->decoding = false;
        }

        /** 
        * GIF Encoder function.
        * Combines the parsed GIF frames into one single animation.
        */
        private function encode($new_filename,$newwidth,$newheight){
            $mystring = "";
            $this->pointer = 0;
            $this->imagedata = array();
            $this->imageinfo = array();
            $this->handle = 0;
            $this->index=0;

            $k=0;
            foreach($this->parsedfiles as $imagepart){
                $this->loadfile($imagepart);
                $this->get_gif_header();
                $this->get_application_data();
                $this->get_comment_data();
                $this->get_graphics_extension(0);
                $this->get_image_block(0);

                //get transparent color index and color
                if(isset($this->encdata[$this->index-1]))
                    $gxdata = $this->encdata[$this->index-1]["graphicsextension"];
                else 
                    $gxdata = null;
                $ghdata = $this->imageinfo["gifheader"];
                $trcolor = "";
                $hastransparency=($gxdata[3]&&1==1);

                if($hastransparency){
                    $trcx = ord($gxdata[6]);
                    $trcolor = substr($ghdata,13+$trcx*3,3);
                }

                //global color table to image data;
                $this->transfercolortable($this->imageinfo["gifheader"],$this->imagedata[$this->index-1]["imagedata"]);	

                $imageblock = &$this->imagedata[$this->index-1]["imagedata"];

                //if transparency exists transfer transparency index
                if($hastransparency){
                    $haslocalcolortable = ((ord($imageblock[9])&128)==128);
                    if($haslocalcolortable){
                        //local table exists. determine boundaries and look for it.
                        $tablesize=(pow(2,(ord($imageblock[9])&7)+1)*3)+10;
                        $this->orgvars[$this->index-1]["transparent_color_index"] = 
                        ((strrpos(substr($this->imagedata[$this->index-1]["imagedata"],0,$tablesize),$trcolor)-10)/3);		
                    }else{
                        //local table doesnt exist, look at the global one.
                        $tablesize=(pow(2,(ord($gxdata[10])&7)+1)*3)+10;
                        $this->orgvars[$this->index-1]["transparent_color_index"] = 
                        ((strrpos(substr($ghdata,0,$tablesize),$trcolor)-10)/3);	
                    }				
                }

                //apply original delay time,transparent index and disposal values to graphics extension

                if(!$this->imagedata[$this->index-1]["graphicsextension"]) $this->imagedata[$this->index-1]["graphicsextension"] = chr(0x21).chr(0xf9).chr(0x04).chr(0x00).chr(0x00).chr(0x00).chr(0x00).chr(0x00);

                $imagedata = &$this->imagedata[$this->index-1]["graphicsextension"];

                $imagedata[3] = chr((ord($imagedata[3]) & 0xE3) | ($this->orgvars[$this->index-1]["disposal_method"] << 2));
                $imagedata[4] = chr(($this->orgvars[$this->index-1]["delay_time"] % 256));
                $imagedata[5] = chr(floor($this->orgvars[$this->index-1]["delay_time"] / 256));
                if($hastransparency){
                    $imagedata[6] = chr($this->orgvars[$this->index-1]["transparent_color_index"]);
                }
                $imagedata[3] = chr(ord($imagedata[3])|$hastransparency);

                //apply calculated left and top offset 
                $imageblock[1] = chr(round(($this->orgvars[$this->index-1]["offset_left"]*$this->wr) % 256));
                $imageblock[2] = chr(floor(($this->orgvars[$this->index-1]["offset_left"]*$this->wr) / 256));
                $imageblock[3] = chr(round(($this->orgvars[$this->index-1]["offset_top"]*$this->hr) % 256));
                $imageblock[4] = chr(floor(($this->orgvars[$this->index-1]["offset_top"]*$this->hr) / 256));			

                if($this->index==1){
                    if(!isset($this->imageinfo["applicationdata"]) || !$this->imageinfo["applicationdata"]) 
                        $this->imageinfo["applicationdata"]=chr(0x21).chr(0xff).chr(0x0b)."NETSCAPE2.0".chr(0x03).chr(0x01).chr(0x00).chr(0x00).chr(0x00);
                    if(!isset($this->imageinfo["commentdata"]) || !$this->imageinfo["commentdata"])
                        $this->imageinfo["commentdata"] = chr(0x21).chr(0xfe).chr(0x10)."PHPGIFRESIZER1.0".chr(0);
                    $mystring .= $this->orgvars["gifheader"]. $this->imageinfo["applicationdata"].$this->imageinfo["commentdata"];
                    if(isset($this->orgvars["hasgx_type_0"]) && $this->orgvars["hasgx_type_0"]) $mystring .= $this->globaldata["graphicsextension_0"];
                    if(isset($this->orgvars["hasgx_type_1"]) && $this->orgvars["hasgx_type_1"]) $mystring .= $this->globaldata["graphicsextension"];
                }

                $mystring .= $imagedata . $imageblock;
                $k++;
                $this->closefile();
            }

            $mystring .= chr(0x3b); 

            //applying new width & height to gif header
            $mystring[6] = chr($newwidth % 256);
            $mystring[7] = chr(floor($newwidth / 256));
            $mystring[8] = chr($newheight % 256);
            $mystring[9] = chr(floor($newheight / 256));
            $mystring[11]= $this->orgvars["background_color"];
            //if(file_exists($new_filename)){unlink($new_filename);}
            file_put_contents($new_filename,$mystring);
        }

        /** 
        * Variable Reset function
        * If a instance is used multiple times, it's needed. Trust me.
        */
        private function clearvariables(){
            $this->pointer = 0;
            $this->index = 0;
            $this->imagedata = array();
            $this->imageinfo = array();            
            $this->handle = 0;
            $this->parsedfiles = array();
        }

        /** 
        * Clear Frames function
        * For deleting the frames after encoding.
        */
        private function clearframes(){
            foreach($this->parsedfiles as $temp_frame){
                unlink($temp_frame);
            }
        }

        /** 
        * Frame Writer
        * Writes the GIF frames into files.
        */
        private function writeframes($prepend){
            for($i=0;$i<sizeof($this->imagedata);$i++){
                file_put_contents($this->temp_dir."/frame_".$prepend."_".str_pad($i,2,"0",STR_PAD_LEFT).".gif",$this->imageinfo["gifheader"].$this->imagedata[$i]["graphicsextension"].$this->imagedata[$i]["imagedata"].chr(0x3b));
                $this->parsedfiles[]=$this->temp_dir."/frame_".$prepend."_".str_pad($i,2,"0",STR_PAD_LEFT).".gif";
            }
        }

        /** 
        * Color Palette Transfer Device
        * Transferring Global Color Table (GCT) from frames into Local Color Tables in animation.
        */
        private function transfercolortable($src,&$dst){
            //src is gif header,dst is image data block
            //if global color table exists,transfer it
            if((ord($src[10])&128)==128){
                //Gif Header Global Color Table Length
                $ghctl = pow(2,$this->readbits(ord($src[10]),5,3)+1)*3;
                //cut global color table from gif header
                $ghgct = substr($src,13,$ghctl);
                //check image block color table length
                if((ord($dst[9])&128)==128){
                    //Image data contains color table. skip.
                }else{
                    //Image data needs a color table.
                    //get last color table length so we can truncate the dummy color table
                    $idctl = pow(2,$this->readbits(ord($dst[9]),5,3)+1)*3;
                    //set color table flag and length	
                    $dst[9] = chr(ord($dst[9]) | (0x80 | (log($ghctl/3,2)-1)));
                    //inject color table
                    $dst = substr($dst,0,10).$ghgct.substr($dst,-1*strlen($dst)+10);
                }
            }else{
                //global color table doesn't exist. skip.
            }
        }

        /** 
        * GIF Parser Functions.
        * Below functions are the main structure parser components.
        */
        private function get_gif_header(){
            $this->p_forward(10);
            if($this->readbits(($mybyte=$this->readbyte_int()),0,1)==1){
                $this->p_forward(2);
                $this->p_forward(pow(2,$this->readbits($mybyte,5,3)+1)*3);
            }else{
                $this->p_forward(2);
            }

            $this->imageinfo["gifheader"]=$this->datapart(0,$this->pointer);
            if($this->decoding){
                $this->orgvars["gifheader"]=$this->imageinfo["gifheader"];
                $this->originalwidth = ord($this->orgvars["gifheader"][7])*256+ord($this->orgvars["gifheader"][6]);
                $this->originalheight = ord($this->orgvars["gifheader"][9])*256+ord($this->orgvars["gifheader"][8]);
                $this->orgvars["background_color"]=$this->orgvars["gifheader"][11];
            }

        }
        //-------------------------------------------------------
        private function get_application_data(){
            $startdata = $this->readbyte(2);
            if($startdata==chr(0x21).chr(0xff)){
                $start = $this->pointer - 2;
                $this->p_forward($this->readbyte_int());
                $this->read_data_stream($this->readbyte_int());
                $this->imageinfo["applicationdata"] = $this->datapart($start,$this->pointer-$start);
            }else{
                $this->p_rewind(2);
            }
        }
        //-------------------------------------------------------
        private function get_comment_data(){
            $startdata = $this->readbyte(2);
            if($startdata==chr(0x21).chr(0xfe)){
                $start = $this->pointer - 2;
                $this->read_data_stream($this->readbyte_int());
                $this->imageinfo["commentdata"] = $this->datapart($start,$this->pointer-$start);
            }else{
                $this->p_rewind(2);
            }
        }
        //-------------------------------------------------------
        private function get_graphics_extension($type){
            $startdata = $this->readbyte(2);
            if($startdata==chr(0x21).chr(0xf9)){
                $start = $this->pointer - 2;
                $this->p_forward($this->readbyte_int());
                $this->p_forward(1);
                if($type==2){
                    $this->imagedata[$this->index]["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
                }else if($type==1){
                    $this->orgvars["hasgx_type_1"] = 1;
                    $this->globaldata["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
                }else if($type==0 && $this->decoding==false){
                    $this->encdata[$this->index]["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
                }else if($type==0 && $this->decoding==true){
                    $this->orgvars["hasgx_type_0"] = 1;
                    $this->globaldata["graphicsextension_0"] = $this->datapart($start,$this->pointer-$start);
                }
            }else{
                $this->p_rewind(2);
            }
        }
        //-------------------------------------------------------
        private function get_image_block($type){
            if($this->checkbyte(0x2c)){
                $start = $this->pointer;
                $this->p_forward(9);
                if($this->readbits(($mybyte=$this->readbyte_int()),0,1)==1){
                    $this->p_forward(pow(2,$this->readbits($mybyte,5,3)+1)*3);
                }
                $this->p_forward(1);
                $this->read_data_stream($this->readbyte_int());
                $this->imagedata[$this->index]["imagedata"] = $this->datapart($start,$this->pointer-$start);

                if($type==0){
                    $this->orgvars["hasgx_type_0"] = 0;
                    if(isset($this->globaldata["graphicsextension_0"]))
                        $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension_0"];
                    else
                        $this->imagedata[$this->index]["graphicsextension"]=null;
                    unset($this->globaldata["graphicsextension_0"]);
                }elseif($type==1){
                    if(isset($this->orgvars["hasgx_type_1"]) && $this->orgvars["hasgx_type_1"]==1){
                        $this->orgvars["hasgx_type_1"] = 0;
                        $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension"];
                        unset($this->globaldata["graphicsextension"]);
                    }else{
                        $this->orgvars["hasgx_type_0"] = 0;
                        $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension_0"];
                        unset($this->globaldata["graphicsextension_0"]);
                    }
                }

                $this->parse_image_data();
                $this->index++;

            }
        }
        //-------------------------------------------------------
        private function parse_image_data(){
            $this->imagedata[$this->index]["disposal_method"] = $this->get_imagedata_bit("ext",3,3,3);
            $this->imagedata[$this->index]["user_input_flag"] = $this->get_imagedata_bit("ext",3,6,1);
            $this->imagedata[$this->index]["transparent_color_flag"] = $this->get_imagedata_bit("ext",3,7,1);
            $this->imagedata[$this->index]["delay_time"] = $this->dualbyteval($this->get_imagedata_byte("ext",4,2));
            $this->imagedata[$this->index]["transparent_color_index"] = ord($this->get_imagedata_byte("ext",6,1));
            $this->imagedata[$this->index]["offset_left"] = $this->dualbyteval($this->get_imagedata_byte("dat",1,2));
            $this->imagedata[$this->index]["offset_top"] = $this->dualbyteval($this->get_imagedata_byte("dat",3,2));
            $this->imagedata[$this->index]["width"] = $this->dualbyteval($this->get_imagedata_byte("dat",5,2));
            $this->imagedata[$this->index]["height"] = $this->dualbyteval($this->get_imagedata_byte("dat",7,2));
            $this->imagedata[$this->index]["local_color_table_flag"] = $this->get_imagedata_bit("dat",9,0,1);
            $this->imagedata[$this->index]["interlace_flag"] = $this->get_imagedata_bit("dat",9,1,1);
            $this->imagedata[$this->index]["sort_flag"] = $this->get_imagedata_bit("dat",9,2,1);
            $this->imagedata[$this->index]["color_table_size"] = pow(2,$this->get_imagedata_bit("dat",9,5,3)+1)*3;
            $this->imagedata[$this->index]["color_table"] = substr($this->imagedata[$this->index]["imagedata"],10,$this->imagedata[$this->index]["color_table_size"]);
            $this->imagedata[$this->index]["lzw_code_size"] = ord($this->get_imagedata_byte("dat",10,1));
            if($this->decoding){
                $this->orgvars[$this->index]["transparent_color_flag"] = $this->imagedata[$this->index]["transparent_color_flag"];
                $this->orgvars[$this->index]["transparent_color_index"] = $this->imagedata[$this->index]["transparent_color_index"];
                $this->orgvars[$this->index]["delay_time"] = $this->imagedata[$this->index]["delay_time"];
                $this->orgvars[$this->index]["disposal_method"] = $this->imagedata[$this->index]["disposal_method"];
                $this->orgvars[$this->index]["offset_left"] = $this->imagedata[$this->index]["offset_left"];
                $this->orgvars[$this->index]["offset_top"] = $this->imagedata[$this->index]["offset_top"];
            }
        }
        //-------------------------------------------------------
        private function get_imagedata_byte($type,$start,$length){
            if($type=="ext")
                return substr($this->imagedata[$this->index]["graphicsextension"],$start,$length);
            elseif($type=="dat")
                return substr($this->imagedata[$this->index]["imagedata"],$start,$length);
        }
        //-------------------------------------------------------
        private function get_imagedata_bit($type,$byteindex,$bitstart,$bitlength){
            if($type=="ext")
                return $this->readbits(ord(substr($this->imagedata[$this->index]["graphicsextension"],$byteindex,1)),$bitstart,$bitlength);
            elseif($type=="dat")
                return $this->readbits(ord(substr($this->imagedata[$this->index]["imagedata"],$byteindex,1)),$bitstart,$bitlength);
        }
        //-------------------------------------------------------
        private function dualbyteval($s){
            $i = ord($s[1])*256 + ord($s[0]);
            return $i;
        }
        //------------   Helper Functions ---------------------
        private function read_data_stream($first_length){
            $this->p_forward($first_length);
            $length=$this->readbyte_int();
            if($length!=0) {
                while($length!=0){
                    $this->p_forward($length);
                    $length=$this->readbyte_int();
                }
            }
            return true;
        }
        //-------------------------------------------------------
        private function loadfile($filename){
            $this->handle = fopen($filename,"rb");
            $this->pointer = 0;
        }
        //-------------------------------------------------------
        private function closefile(){
            fclose($this->handle);
            $this->handle=0;
        }
        //-------------------------------------------------------
        private function readbyte($byte_count){
            $data = fread($this->handle,$byte_count);
            $this->pointer += $byte_count;
            return $data;
        }
        //-------------------------------------------------------
        private function readbyte_int(){
            $data = fread($this->handle,1);
            $this->pointer++;
            return ord($data);
        }
        //-------------------------------------------------------
        private function readbits($byte,$start,$length){
            $bin = str_pad(decbin($byte),8,"0",STR_PAD_LEFT);
            $data = substr($bin,$start,$length);
            return bindec($data);
        }
        //-------------------------------------------------------
        private function p_rewind($length){
            $this->pointer-=$length;
            fseek($this->handle,$this->pointer);
        }
        //-------------------------------------------------------
        private function p_forward($length){
            $this->pointer+=$length;
            fseek($this->handle,$this->pointer);
        }
        //-------------------------------------------------------
        private function datapart($start,$length){
            fseek($this->handle,$start);
            $data = fread($this->handle,$length);
            fseek($this->handle,$this->pointer);
            return $data;
        }
        //-------------------------------------------------------
        private function checkbyte($byte){
            if(fgetc($this->handle)==chr($byte)){
                fseek($this->handle,$this->pointer);
                return true;
            }else{
                fseek($this->handle,$this->pointer);
                return false;
            }
        }	
        //-------------------------------------------------------
        private function checkEOF(){
            if(fgetc($this->handle)===false){
                return true;
            }else{
                fseek($this->handle,$this->pointer);
                return false;
            }
        }	
        //-------------------------------------------------------
        /** 
        * Debug Functions. 
        * Parses the GIF animation into single frames.
        */
        private function debug($string){
            echo "<pre>";
            for($i=0;$i<strlen($string);$i++){
                echo str_pad(dechex(ord($string[$i])),2,"0",STR_PAD_LEFT). " ";
            }
            echo "</pre>";
        }
        //-------------------------------------------------------
        private function debuglen($var,$len){
            echo "<pre>";
            for($i=0;$i<$len;$i++){
                echo str_pad(dechex(ord($var[$i])),2,"0",STR_PAD_LEFT). " ";
            }
            echo "</pre>";
        }	
        //-------------------------------------------------------
        private function debugstream($length){
            $this->debug($this->datapart($this->pointer,$length));
        }
        //-------------------------------------------------------
        /** 
        * GD Resizer Device
        * Resizes the animation frames
        */
        private function resizeframes(){
            $k=0;
            foreach($this->parsedfiles as $img){
                $src = imagecreatefromgif($img);
                $sw = $this->imagedata[$k]["width"];
                $sh = $this->imagedata[$k]["height"];
                $nw = round($sw * $this->wr);
                $nh = round($sh * $this->hr);
                $sprite = imagecreatetruecolor($nw,$nh);	
                $trans = imagecolortransparent($sprite);
                imagealphablending($sprite, false);
                imagesavealpha($sprite, true);
                imagepalettecopy($sprite,$src);					
                imagefill($sprite,0,0,imagecolortransparent($src));
                imagecolortransparent($sprite,imagecolortransparent($src));						
                imagecopyresized($sprite,$src,0,0,0,0,$nw,$nh,$sw,$sh);		
                imagegif($sprite,$img);
                imagedestroy($sprite);
                imagedestroy($src);
                $k++;
            }
        }
    }


?>

ASCII字符画样式的文本验证码

ASCII CAPTCHA类是一种创新的方式来保护您的网站免收垃圾信息的困扰。对于无法处理显示图形输出的标准CAPTCHA的服务器或浏览器,ASCII CAPTCHA是一种创新且仍然安全的方式,可以为CAPTCHA提供ASCII艺术。

个人觉得这个验证码比很多图形验证码更难被机器识别!:)

用法示例

<?php 
$captcha = new ASCII_Captcha(); 
$data = $captcha->create($text); 
echo "<b>$text</b> :<br><pre style='font-size:8px;'>$data</pre>" 
?> 
显示效果

bgchar属性:在此属性中,您可以定义背景应该是单个字符。请注意,后台可以使用以下属性中定义的随机字符覆盖:

repset属性: 此“repset”属性定义可替换背景的字符数组,以进一步模糊输出。

repset_count属性: 此属性用于定义应在repset池中输入的背景字符数量(请参阅random_rep()方法)。

length属性:此属性定义要使用的字符数。

spacing属性:字母之间的间距,按字符数计算。

使用时需要包含下面的类:

<?php

Class ASCII_Captcha
	{
		var $fonts;
		var $bgchar = ".";
		var $length = 6;
		var $spacing = 3;
		var $repset = array('*', "!", "~");
		var $repset_count = 10;
		var $parent;
		
		function ASCII_Captcha()
			{
				$fonts[0]['A'] = 
"   ###    
  ##&##   
 ##&&&##  
##&&&&&## 
######### 
##&&&&&## 
##&&&&&## ";
				$fonts[0]['B'] = 
"########  
##&&&&&## 
##&&&&&## 
########  
##&&&&&## 
##&&&&&## 
########  ";
				$fonts[0]['C'] = 
" ######  
##&&&&## 
##       
##       
##       
##&&&&## 
 ######  ";
 				$fonts[0]['D'] = 
"########  
##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&&&&## 
########  ";
				$fonts[0]['E'] = 
"######## 
##       
##       
######   
##       
##       
######## ";
				$fonts[0]['F'] = 
"######## 
##       
##       
######   
##       
##       
##       ";
				$fonts[0]['G'] = 
" ######   
##&&&&##  
##        
##&&&#### 
##&&&&##  
##&&&&##  
 ######   ";
				$fonts[0]['H'] = 
"##&&&&&## 
##&&&&&## 
##&&&&&## 
######### 
##&&&&&## 
##&&&&&## 
##&&&&&## ";
				$fonts[0]['I'] = 
"#### 
 ##  
 ##  
 ##  
 ##  
 ##  
#### ";
				$fonts[0]['J'] = 
"      ## 
      ## 
      ## 
      ## 
##&&&&## 
##&&&&## 
 ######  ";
				$fonts[0]['K'] = 
"##&&&&## 
##&&&##  
##&&##   
#####    
##&&##   
##&&&##  
##&&&&## ";
				$fonts[0]['L'] = 
"##       
##       
##       
##       
##       
##       
######## ";
				$fonts[0]['M'] = 
"##&&&&&## 
###&&&### 
####&#### 
##&###&## 
##&&&&&## 
##&&&&&## 
##&&&&&## ";
				$fonts[0]['N'] = 
"##&&&&## 
###&&&## 
####&&## 
##&##&## 
##&&#### 
##&&&### 
##&&&&## ";
				$fonts[0]['O'] = 
" #######  
##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&&&&## 
 #######  ";
				$fonts[0]['P'] = 
"########  
##&&&&&## 
##&&&&&## 
########  
##        
##        
##        ";
				$fonts[0]['Q'] = 
" #######  
##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&##&## 
##&&&&##  
 #####&## ";
				$fonts[0]['R'] = 
"########  
##&&&&&## 
##&&&&&## 
########  
##&&&##   
##&&&&##  
##&&&&&## ";
				$fonts[0]['S'] = 
" ######  
##&&&&## 
##       
 ######  
      ## 
##&&&&## 
 ######  ";
				$fonts[0]['T'] = 
"######## 
   ##    
   ##    
   ##    
   ##    
   ##    
   ##    ";
				$fonts[0]['U'] = 
"##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&&&&## 
 #######  ";
				$fonts[0]['V'] = 
"##&&&&&## 
##&&&&&## 
##&&&&&## 
##&&&&&## 
 ##&&&##  
  ##&##   
   ###    ";
				$fonts[0]['W'] = 
"##&&&&&&## 
##&&##&&## 
##&&##&&## 
##&&##&&## 
##&&##&&## 
##&&##&&## 
 ###&&###  ";
				$fonts[0]['X'] = 
"##&&&&&## 
 ##&&&## 
  ##&##   
   ###    
  ##&##   
 ##&&&##  
##&&&&&## ";
				$fonts[0]['Y'] = 
"##&&&&## 
 ##&&##  
  ####   
   ##    
   ##    
   ##    
   ##    ";
				$fonts[0]['Z'] = 
"######## 
     ##  
    ##   
   ##    
  ##     
 ##      
######## ";
				$fonts[0]['0'] = 
" #######  
##&&&&&## 
##&&&#&## 
##&&#&&## 
##&#&&&## 
##&&&&&## 
 #######  ";
				$fonts[0]['1'] = 
"   ##   
 ####   
   ##   
   ##   
   ##   
   ##   
 ###### ";
				$fonts[0]['2'] = 
" #######  
##&&&&&## 
       ## 
 #######  
##        
##        
######### ";
				$fonts[0]['3'] = 
" #######  
##&&&&&## 
       ## 
 #######  
       ## 
##&&&&&## 
 #######  ";
				$fonts[0]['4'] = 
"##
##&&&&##  
##&&&&##  
##&&&&##  
######### 
      ##  
      ##  ";
				$fonts[0]['5'] = 
"######## 
##       
##       
#######  
      ## 
##&&&&## 
 ######  ";
				$fonts[0]['6'] = 
" #######  
##&&&&&## 
##        
########  
##&&&&&## 
##&&&&&## 
 #######  ";
				$fonts[0]['7'] = 
"######## 
##&&&&## 
    ##   
   ##    
  ##     
  ##     
  ##     ";
				$fonts[0]['8'] = 
" #######  
##&&&&&## 
##&&&&&## 
 #######  
##&&&&&## 
##&&&&&## 
 #######  ";
				$fonts[0]['9'] = 
" #######  
##&&&&&## 
##&&&&&## 
 ######## 
       ## 
##&&&&&## 
 #######  ";

				$this->fonts = $fonts;
			}
		
		function create(&$text)
			{
				$text = $this->make($captcha);
				return $captcha;
			}
		
		function bg_distort($string)
			{
				while (substr_count($string, $this->bgchar . $this->bgchar . $this->bgchar . $this->bgchar . $this->bgchar . $this->bgchar))
					{
						$rep = "";
						for ($x = 6;$x > 0;$x--)
							{
								$rep .= $this->random_rep();
							};
						$string = $this->str_replace_once($this->bgchar . $this->bgchar . $this->bgchar . $this->bgchar . $this->bgchar . $this->bgchar, $rep, $string);
					};
				return $string;
			}
		
		function random_rep()
			{
				$repset = $this->repset;
				$repset_count = $this->repset_count;
				for (;$repset_count > 0;$repset_count--)
					{
						$repset[] = $this->bgchar;
					};
				$v = $repset[rand(0, count($repset) - 1)];
				return $v;
			}
		
		function str_replace_once($search, $replace, $subject)
			{
			    if (($pos = strpos($subject, $search)) !== false)
					{
						$ret = substr($subject, 0, $pos).$replace.substr($subject, $pos + strlen($search));
					}
			    else
				    {
						$ret = $subject;
					};
			    return($ret);
			}
		
		function make(&$captcha)
			{
				$string = "";
				$captcha = "";
				for ($x = $this->length;$x > 0;$x--)
					{
						$letter_data = $this->random_letter($letter);
						$captcha = $this->new_letter($captcha, $letter_data);
						$string .= $letter;
					};
				$captcha = $this->touchup($captcha);
				$captcha = $this->bg_distort($captcha);
				return $string;
			}
		
		function touchup($data)
			{
				$data = explode("\n", $data);
				$len = strlen($data[0]);
				$wrapper = "";
				for (;$len > 0;$len--)
					{
						$wrapper .= $this->bgchar;
					};
				$data = implode("\n", $data);
				$data = $wrapper . "\n" . $data . "\n" . $wrapper;
				return $data;
			}
		
		function new_letter($current, $new)
			{
				$new = $this->parse_letter($new);
				$new = explode("\n", $new);
				$current = explode("\n", $current);
                                $current = $current!='' ? explode("\n", $current) : array_fill(0,7,'');
				foreach ($new as $n => $w)
					{
						$current[$n] .= $w;
					};
				return implode("\n", $current);
			}
		
		function distort($letter_data)
			{
				if (rand(0, 1))
					{
						$rand = rand(0, count($letter_data) - 1);
						$letter_data[$rand] = " " . $letter_data[$rand];
					};
				return $letter_data;
			}
		
		function parse_letter($letter_data)
			{
				$letter_data = explode("\n", $letter_data);
				$letter_data = $this->distort($letter_data);
				foreach ($letter_data as $line_num => $line)
					{
						$letter_data[$line_num] = $this->trim_left($line, $this->bgchar);
						$letter_data[$line_num] = $this->trim_right($letter_data[$line_num], $this->bgchar);
						$letter_data[$line_num] = str_replace("&", $this->bgchar, $letter_data[$line_num]);
					};
				$letter_data = $this->clean_right($letter_data, $this->bgchar);
				$letter_data = $this->wrap($letter_data, $this->bgchar);
				return implode("\n", $letter_data);
			}
		
		function random_letter(&$picked_letter)
			{
				$letters = $this->fonts[rand(0, count($this->fonts) - 1)];
				$pick = rand(0, count($letters) - 1);
				$cur = 0;
				foreach ($letters as $letter => $data)
					{
						if ($cur == $pick)
							{
								$picked_letter = $letter;
								$return = $data;
								break;
							}
						else
							{
								$cur++;
							};
					};
				return $return;
			}
		
		function str_replace_left($search, $replace, $subject)
			{
			    if (($pos = @strpos($subject, $search)) !== FALSE)
			 		{
			     	   $ret = substr($subject, 0, $pos).$replace.substr($subject, $pos + strlen($search));
					}
			    else
					{
			     	   $ret = $subject;
			   		};
			    return $ret;
			}
		
		function str_replace_right($search, $replace, $subject)
			{
				$subject = strrev($subject);
				$search = strrev($search);
				$replace = strrev($replace);
				if (($pos = @strpos($subject, $search)) !== FALSE)
			 		{
			     	   $ret = substr($subject, 0, $pos).$replace.substr($subject, $pos + strlen($search));
					}
			    else
					{
			     	   $ret = $subject;
			   		};
			   	$ret = strrev($ret);
			    return($ret);
			}
	
		function wrap($letters, $character)
			{
				$prefix = "";
				$quantity = $this->spacing;
				for (;$quantity > 0; $quantity--)
					{
						$prefix .= $character;
					};
				foreach ($letters as $lnum => $line)
					{
						$letters[$lnum] = $prefix . $letters[$lnum] . $prefix;
					};
				return $letters;
			}
	
		function clean_right($array, $char)
			{
				$longest = 0;
				foreach ($array as $line)
					{
						if (strlen($line) > $longest)
							{
								$longest = strlen($line);
							};
					};
				foreach ($array as $lnum => $line)
					{
						if (strlen($line) < $longest)
							{
								$append = "";
								for ($x = $longest - strlen($line);$x > 0;$x--)
									{
										$append .= $char;
									};
								$array[$lnum] .= $append;
							};
					};
				return $array;
			}
		
		function trim_right($string, $char)
			{
				if (preg_match('/(\\s*)$/', $string, $regs))
					{
						$match = $regs[1];
						$replace = "";
						for ($x = strlen($match);$x > 0;$x--)
							{
								$replace .= $char;
							};
						$string = $this->str_replace_right($match, $replace, $string);
					};
				return $string;
			}
		
		function trim_left($string, $char)
			{
				if (preg_match('/^(\\s*)/', $string, $regs))
					{
						$match = $regs[1];
						$replace = "";
						for ($x = strlen($match);$x > 0;$x--)
							{
								$replace .= $char;
							};
						$string = $this->str_replace_left($match, $replace, $string);
					};
				return $string;
			}
	};

?>