PHP 5.0.1 中 GD2.0.28 支持创建 GIF 图像了,我测试了下,确实如此。附上我写的一个图片验证代码,我乱封装成Class了,其实用function就可以了。自己修改吧,呵呵。
<?php
/**
* @author ShenKong <[email protected]>
* @version $id
* @package SPB Forum System
* @copyright 深空(shenkong)版权所有,任何人修改和使用必须保留此版权信息
*/
class CheckImage
{
public $bgLength;
public $bgWidth;
public $bgColor;
public $fontSize;
public $fontType;
public $length;
public $image;
public $string;
function __construct($info)
{
$this->bgLength = $info["bgLength"];
$this->bgWidth = $info["bgWidth"];
$this->fontSize = $info["fontSize"];
$this->fontType = $info["fontType"];
$this->length = $info["length"];
$this->image = $this->createImage();
//$black = imagecolorallocate($this->image, 0x00, 0x00, 0x00);
$white = imagecolorallocate($this->image, 0xFF, 0xFF, 0xFF);
imagefilledrectangle($this->image, 0, 0, $this->bgLength, $this->bgWidth, $white);
$this->mixImage(500, 10);
$this->write();
$this->mixImage(50, 5);
// 设置白色为透明
imagecolortransparent($this->image, $white);
header("Content-Type: image/gif");
imagegif($this->image);
}
function createImage()
{
return imagecreatetruecolor($this->bgLength, $this->bgWidth);
}
function randColor()
{
return imagecolorallocate($this->image, mt_rand(0, 200), mt_rand(0, 100), mt_rand(0, 150));
}
public function write()
{
$str = md5(mt_rand());
$str = strtoupper($str);
$str = str_replace(array("I", "1", "5", "S", "4", "A", "8", "B", "0", "D"), "", $str);
$str = substr($str, 0, $this->length);
$this->string = $str;
$w = ($this->bgLength - ($this->length - 1) * 3) / $this->length;
for ($i = 0; $i < $this->length; $i++)
{
imagettftext($this->image, $this->fontSize, mt_rand(-20, 20), mt_rand($i * $w + $i * 3, ($i + 1) * $w + $i * 3 - $this->fontSize), mt_rand($this->fontSize, $this->bgWidth - $this->bgWidth / 10), $this->randColor(), $this->fontType, substr($str, $i, 1));
}
}
function setString()
{
require_once "Session.class.php";
$s = new Session();
$s->setPath();
}
function mixImage($num = 500, $fontSize = 10)
{
for ($i = 0; $i < $num; $i++)
{
imagettftext($this->image, $fontSize, mt_rand(-180, 180), mt_rand(1, $this->bgLength), mt_rand(1, $this->bgWidth), imagecolorallocate($this->image, mt_rand(150, 255), mt_rand(150, 255), mt_rand(150, 255)), $this->fontType, "|");
}
}
}
$info = array (
"bgLength" => 90,
"bgWidth" => 30,
"fontSize" => 18,
"fontType" => "Arial",
"length" => 5
);
new CheckImage($info);
?>
本文地址:http://com.8s8s.com/it/it28563.htm