- <?php
- header("content-type: p_w_picpath/png");
- class yanzhengma{
- public $str="你是我的好朋友"; //验证码
- public $geshu=4;//验证码个数
- public $img;//图片sourcer
- public function __construct($geshu){
- $this->geshu=$geshu; //初始化个数
- }
- public function yanzhengma($x,$y){
- //创建一个画布x表示横坐标,y表示纵坐标
- $this->img=p_w_picpathcreatetruecolor($x,$y);//创建画布,返回画布句柄
- $rand = strlen($this->str)-2;
- $rand = $rand/2;//中文占2个字节
- $text_color=p_w_picpathcolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//随机创建文本的颜色
- $text_color2=p_w_picpathcolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//随机创建背景的颜色
- $text='';
- for($i=0;$i<$this->geshu;$i++){
- $rand=mt_rand(0,$rand);//得到验证码
- $text=$text.substr($this->str,$rand*2,2);
- }
- $text=iconv('gb2312', 'UTF-8', $text);//必须转换才能用
- p_w_picpathfill($this->img, 0, 0, $text_color2);//填充背景
- p_w_picpathttftext ($this->img,20,1,10,30,$text_color,"simkai.ttf",$text);//这个函数用于输出中文
- //p_w_picpathstring($img, 30, 40, 40, $text, $text_color);该函数只能用于英文数字
- p_w_picpathpng($this->img);//显示图片
- return $text;//返回验证码,用于以后session
- }
- public function destory(){
- p_w_picpathdestroy($this->img);//一定要关闭资源
- }
- }
- $img= new yanzhengma(5);
- $text=$img->yanzhengma(200,40);
- file_put_contents('1.txt', $text) ;
- $img->destory();
- ?>