PHP 5.0.1 中 GD2.0.28 支持创建 GIF 图像了[附一个图片验证程序]

王朝php·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

[url=http://www.php.net][url=http://www.php.net][url=http://www.php.net]PHP 5.0.1 中 GD2.0.28 支持创建 GIF 图像了,我测试了下,确实如此。附上我写的一个图片验证代码,我乱封装成Class了,其实用function就可以了。自己修改吧,呵呵。

<?php

/**

* @author ShenKong <shenkong@openphp.cn>

* @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);

?>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航