加粗或者斜體的文字可以用php的函數控制.我想妳是想生成驗證碼圖片是嗎?
如果是想生成驗證麽圖片有幾個函數可以考慮
imagecreate($length,$height)創建圖片.參數是圖片的寬度和高度
imagecolorallocate($image,$r,$g,$b)設置背景色,r b g就是圖片的三色rgb參數.這個可以由傳入0-255的隨機數決定隨機的背景色.還可以生成字體色
imagettftext($_image,$fontSize,?mt_rand(-40,?70),?$codeNX,$fontSize*1.5,$_color,?$ttf,?$code[$i]);寫入隨機的文字,這裏要壹個字壹個字寫.所以這個函數要循環調用.
百度了壹下 找到了壹個類...如下
<?php/**
*?安全驗證碼 *? *?安全的驗證碼要:驗證碼文字扭曲、旋轉,使用不同字體,添加幹擾碼。 *?如果用中文做驗證碼(我這裏不是哦,有興趣妳來改成用中文的),安全度會更好些,但驗證碼扭曲和旋轉是王道,用了字體也算是已經給字體扭曲了,我就不再去給他添壹只扭曲的足了。 *?可配置的屬性都是壹些簡單直觀的變量,我就不用弄壹堆的setter/getter了 * *?@author?流水孟春?<cmpan(at)qq.com> *?@copyright?NEW?BSD *?@link?/YL_Security_Secoder *?@link?/docs/yl/security/secoder */class?YL_Security_Secoder?{
/**
?*?驗證碼的session的下標
?*?
?*?@var?string
?*/
public?static?$seKey?=?'sid.sekey.ylans.cn';
public?static?$expire?=?3000;?//?驗證碼過期時間(s)
/**
?*?驗證碼中使用的字符,01IO容易混淆,建議不用
?*
?*?@var?string
?*/
public?static?$codeSet?=?'346789ABCDEFGHJKLMNPQRTUVWXY';
public?static?$fontSize?=?25;?//?驗證碼字體大小(px)
public?static?$useCurve?=?true;//?是否畫混淆曲線
public?static?$useNoise?=?true;//?是否添加雜點
public?static?$imageH?=?0;//?驗證碼圖片寬
public?static?$imageL?=?0;//?驗證碼圖片長
public?static?$length?=?4;//?驗證碼位數
public?static?$bg?=?array(243,?251,?254);?//?背景
protected?static?$_image?=?null;?//?驗證碼圖片實例
protected?static?$_color?=?null;?//?驗證碼字體顏色
/**
?*?輸出驗證碼並把驗證碼的值保存的session中
?*?驗證碼保存到session的格式為:?$_SESSION[self::$seKey]?=?array('code'?=>?'驗證碼值',?'time'?=>?'驗證碼創建時間');
?*/
public?static?function?entry()?{
//?圖片寬(px)
self::$imageL?||?self::$imageL?=?self::$length?*?self::$fontSize?*?1.5?+?self::$fontSize*1.5;?
//?圖片高(px)
self::$imageH?||?self::$imageH?=?self::$fontSize?*?2;
//?建立壹幅?self::$imageL?x?self::$imageH?的圖像
self::$_image?=?imagecreate(self::$imageL,?self::$imageH);?
//?設置背景?
imagecolorallocate(self::$_image,?self::$bg[0],?self::$bg[1],?self::$bg[2]);?
//?驗證碼字體隨機顏色
self::$_color?=?imagecolorallocate(self::$_image,?mt_rand(1,120),?mt_rand(1,120),?mt_rand(1,120));
//?驗證碼使用隨機字體?
$ttf?=?dirname(__FILE__)?.?'/ttfs/'?.?mt_rand(1,?20)?.?'.ttf';?
if?(self::$useNoise)?{
//?繪雜點
self::_writeNoise();
}?
if?(self::$useCurve)?{
//?繪幹擾線
self::_writeCurve();
}
//?繪驗證碼
$code?=?array();?//?驗證碼
$codeNX?=?0;?//?驗證碼第N個字符的左邊距
for?($i?=?0;?$i<self::$length;?$i++)?{
$code[$i]?=?self::$codeSet[mt_rand(0,?27)];
$codeNX?+=?mt_rand(self::$fontSize*1.2,?self::$fontSize*1.6);
//?寫壹個驗證碼字符
imagettftext(self::$_image,?self::$fontSize,?mt_rand(-40,?70),?$codeNX,?self::$fontSize*1.5,?self::$_color,?$ttf,?$code[$i]);
}
//?保存驗證碼
isset($_SESSION)?||?session_start();
$_SESSION[self::$seKey]['code']?=?join('',?$code);?//?把校驗碼保存到session
$_SESSION[self::$seKey]['time']?=?time();?//?驗證碼創建時間
header('Cache-Control:?private,?max-age=0,?no-store,?no-cache,?must-revalidate');
header('Cache-Control:?post-check=0,?pre-check=0',?false);
header('Pragma:?no-cache');
header("content-type:?image/png");
//?輸出圖像
imagepng(self::$_image);?
imagedestroy(self::$_image);
}
/**?
?*?畫壹條由兩條連在壹起構成的隨機正弦函數曲線作幹擾線(妳可以改成更帥的曲線函數)?
*? *?高中的數學公式咋都忘了涅,寫出來?* 正弦型函數解析式:y=Asin(ωx+φ)+b
?*?各常數值對函數圖像的影響:
?*A:決定峰值(即縱向拉伸壓縮的倍數)
?*b:表示波形在Y軸的位置關系或縱向移動距離(上加下減)
?*φ:決定波形與X軸位置關系或橫向移動距離(左加右減)
?*ω:決定周期(最小正周期T=2π/∣ω∣)
?*
?*/
protected?static?function?_writeCurve()?{
$A?=?mt_rand(1,?self::$imageH/2);?//?振幅
$b?=?mt_rand(-self::$imageH/4,?self::$imageH/4);//?Y軸方向偏移量
$f?=?mt_rand(-self::$imageH/4,?self::$imageH/4);//?X軸方向偏移量
$T?=?mt_rand(self::$imageH*1.5,?self::$imageL*2);?//?周期
$w?=?(2*?M_PI)/$T;
$px1?=?0;?//?曲線橫坐標起始位置
$px2?=?mt_rand(self::$imageL/2,?self::$imageL?*?0.667);?//?曲線橫坐標結束位置?
for?($px=$px1;?$px<=$px2;?$px=$px+?0.9)?{
if?($w!=0)?{
$py?=?$A?*?sin($w*$px?+?$f)+?$b?+?self::$imageH/2;?//?y?=?Asin(ωx+φ)?+?b
$i?=?(int)?((self::$fontSize?-?6)/4);
while?($i?>?0)?{
imagesetpixel(self::$_image,?$px?+?$i,?$py?+?$i,?self::$_color);?//?這裏畫像素點比imagettftext和imagestring性能要好很多
$i--;
}
}
}
$A?=?mt_rand(1,?self::$imageH/2);?//?振幅
$f?=?mt_rand(-self::$imageH/4,?self::$imageH/4);//?X軸方向偏移量
$T?=?mt_rand(self::$imageH*1.5,?self::$imageL*2);?//?周期
$w?=?(2*?M_PI)/$T;
$b?=?$py?-?$A?*?sin($w*$px?+?$f)?-?self::$imageH/2;
$px1?=?$px2;
$px2?=?self::$imageL;
for?($px=$px1;?$px<=$px2;?$px=$px+?0.9)?{
if?($w!=0)?{
$py?=?$A?*?sin($w*$px?+?$f)+?$b?+?self::$imageH/2;?//?y?=?Asin(ωx+φ)?+?b
$i?=?(int)?((self::$fontSize?-?8)/4);
while?($i?>?0)?{
imagesetpixel(self::$_image,?$px?+?$i,?$py?+?$i,?self::$_color);?//?這裏(while)循環畫像素點比imagettftext和imagestring用字體大小壹次畫出(不用這while循環)性能要好很多
$i--;
}
}
}
}
/**
?*?畫雜點
?*?往圖片上寫不同顏色的字母或數字
?*/
protected?static?function?_writeNoise()?{
for($i?=?0;?$i?<?10;?$i++){
//雜點顏色
$noiseColor?=?imagecolorallocate(
?self::$_image,?
?mt_rand(150,225),?
?mt_rand(150,225),?
?mt_rand(150,225)
?);
for($j?=?0;?$j?<?5;?$j++)?{
//?繪雜點
imagestring(
self::$_image,
5,?
mt_rand(-10,?self::$imageL),?
mt_rand(-10,?self::$imageH),?
self::$codeSet[mt_rand(0,?27)],?//?雜點文本為隨機的字母或數字
$noiseColor
);
}
}
}
/**
?*?驗證驗證碼是否正確
?*
?*?@param?string?$code?用戶驗證碼
?*?@return?bool?用戶驗證碼是否正確
?*/
public?static?function?check($code)?{
isset($_SESSION)?||?session_start();
//?驗證碼不能為空
if(empty($code)?||?empty($_SESSION[self::$seKey]))?{
return?false;
}
//?session?過期
if(time()?-?$_SESSION[self::$seKey]['time']?>?self::$expire)?{
unset($_SESSION[self::$seKey]);
return?false;
}
if($code?==?$_SESSION[self::$seKey]['code'])?{
return?true;
}
return?false;
}
}
//?useage
/*
YL_Security_Secoder::$useNoise?=?false;?//?要更安全的話改成true
YL_Security_Secoder::$useCurve?=?true;
YL_Security_Secoder::entry();
*/
/*
//?驗證驗證碼
if?(!YL_Security_Secoder::check(@$_POST['secode']))?{
print?'error?secode';
}
*/
這是效果