以下内容节选自《php web2.0开发实战》 老外写国人译的书
可以使用pear(php扩展与应用库)提供的text_captcha组件来生成captcha图象。
先安装:可以下载,也可用pear命令
pear install text_password
pear install -f text_captcha
pear install -f image_text
这个包依赖于另外两个,我试了一下,主要是生成随机的字符用的
还需要一个字体文件,后缀是ttf,网上很多,
// 以下是createimage.php include(text_captcha文件); //如果include路径已指向pear目录,可以没有这句话 $captcha = text_captcha::factory('image'); //生成对象先 //以下依次是大小,字库文件的路径,字库文件名 $opts = array('font_size' => 20, 'font_path' => zend_registry::get('config')->paths->data, 'font_file' => 'verabd.ttf'); $phare = 自定义一个随机数; $captcha->init(120, 60, $phrase, $opts); //120和60是生成图片的宽和高 //这里我选择png图片,随意,也可用jpg header('content-type: image/png'); echo $captcha->getcaptchaaspng();
//以下是test.html
是不是很简单?82.95.186.47:9001 850246527