- 论坛徽章:
- 0
|
linux系统为fedora core 3
图片是这样的:现存一个jpg,然后在上面加上随机的几个字符,图片的链接还是php.
-----------------------------------------------------------------------------------
某页面图片链接<img src="SecuImage.php">;
-----------------------------------------------
SecuImage.php文件
<?
include("securityImageClass.php" ;
$si = new securityImage();
$si->;showImage();
?>;
-----------------------------------------------
securityImageClass.php文件
<?
class securityImage {
var $inputParam = ""; // Public;
var $name = "security"; // Public;
var $codeLength = 5; // Private;
var $fontSize = 6; // Private;
var $fontColor = "000000"; // Private; FFFFFF白色 000000黑色
var $imageFile = "map.jpg"; // Private; MUST BE JPEG FILE!
var $securityCode = ""; // Private
function securityImage()
{
session_start();
}
function simpleRandString($length=16, $list="23456789ABCDEFGHIJKMNPRSTUVWXYZ"
{
/*
* Generates a random string with the specified length
* Chars are chosen from the provided [optional] list
*/
mt_srand((double)microtime()*1000000);
$newstring = "";
if ($length >; 0) {
while (strlen($newstring) < $length) {
$newstring .= $list[mt_rand(0, strlen($list)-1)];
}
}
return $newstring;
}
function showImage() {
header("Content-type: image/jpeg" ;
$this->;generateImage();
imagejpeg($this->;img);
imageDestroy($this->;img);
}
function generateImage() {
$this->;securityCode = $this->;simpleRandString($this->;codeLength);
$_SESSION['security_code'] = $this->;securityCode;
$img_path = dirname(__FILE__)."/$this->;imageFile";
$this->;img = ImageCreateFromJpeg($img_path);
$img_size = getimagesize($img_path);
$color = imagecolorallocate($this->;img,
hexdec(substr($this->;fontColor, 1, 2)),
hexdec(substr($this->;fontColor, 3, 2)),
hexdec(substr($this->;fontColor, 5, 2))
);
$fw = imagefontwidth($this->;fontSize);
$fh = imagefontheight($this->;fontSize);
// create a new string with a blank space between each letter so it looks better
$newstr = "";
for ($i = 0; $i < strlen($this->;securityCode); $i++) {
$newstr .= $this->;securityCode[$i] ." ";
}
// remove the trailing blank
$newstr = trim($newstr);
// center the string
$x = ($img_size[0] - strlen($newstr) * $fw ) / 2;
// output each character at a random height and standard horizontal spacing
for ($i = 0; $i < strlen($newstr); $i++) {
$hz = mt_rand( 10, $img_size[1] - $fh - 5);
imagechar( $this->;img, $this->;fontSize, $x + ($fw*$i), $hz, $newstr[$i], $color);
}
}
}
?>; |
|