PHP高自定義性安全驗(yàn)證碼代碼
更新時(shí)間:2011年11月27日 00:22:28 作者:
根據(jù)網(wǎng)上的一個(gè)驗(yàn)證碼修改。 自定義性非常強(qiáng),需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
<?php
public function captcha(){
$font_dir = $_SERVER ["DOCUMENT_ROOT"] . "your_ttf_file.ttf"; // 字體庫(kù)
$img_w = 58; // 設(shè)置圖片寬
$img_h = 20; // 設(shè)置圖片高
$font_size = 11; // 字體大小
$angle_l = -10; // 左偏角
$angle_r = 10; // 右偏角
$code_str = "ABCDEFGHJKLMNPQRSTUVWXYZ36";
$word_len = 4; // 驗(yàn)證碼位數(shù)
$padding = 5; // 每?jī)蓚€(gè)文字之間間隔
$margin = 2; // 左側(cè)邊距
$base_line = 15; // 文字基線(xiàn)位置
$base_line_offset = 2; // 基準(zhǔn)線(xiàn)偏移量
$pixel_num = 3; // 雜點(diǎn)數(shù)目基數(shù)
$pixel_color= 8; // 雜點(diǎn)只有 $pixel_color 種顏色 總的雜點(diǎn)數(shù)為$pixel_num*$pixel_color
$noise_font_size = 1; // 雜點(diǎn)字體大小
$session_key= "my.xoyo_captcha"; //自定義session鍵名
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
header("Cache-control: private");
header('Content-Type: image/png');
session_start();
$word = "";
$code_str_len = strlen($code_str) - 1;
for ($i = 0; $i < $word_len; $i++) {
$word.= $code_str[rand(0, $code_str_len)];
}
$_SESSION [$session_key] = strtolower($word);
$image = imagecreatetruecolor($img_w, $img_h);
imagefilledrectangle($image, 0, 0, $img_w - 1, $img_h - 1, imagecolorallocate($image, mt_rand(235, 255), mt_rand(235, 255), mt_rand(235, 255)));
//繪制雜點(diǎn)
for($i = 0; $i < $pixel_color; $i++){
$noise_color = imagecolorallocate( $image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) );
for($j = 0; $j < $pixel_num; $j++) {
imagestring( $image, $noise_font_size, mt_rand(-10, $img_w), mt_rand(-10, $img_h), $code_str[mt_rand(0, $code_str_len)], $noise_color );
}
}
//繪制文字
for ($i = 0; $i < $word_len; ++$i) {
$color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));
imagettftext($image, $font_size, mt_rand($angle_l, $angle_r), $margin, mt_rand($base_line-$base_line_offset, $base_line+$base_line_offset), $color, $font, mb_substr($word, $i, 1, 'utf-8'));
$margin += (imagefontwidth($font_size) + $padding);
}
imagepng($image);
imagedestroy($image);
exit;
}
您可能感興趣的文章:
- PHP自定義大小驗(yàn)證碼的方法詳解
- 如何用php生成扭曲及旋轉(zhuǎn)的驗(yàn)證碼圖片
- 支持中文字母數(shù)字、自定義字體php驗(yàn)證碼代碼
- PHP 驗(yàn)證碼的實(shí)現(xiàn)代碼
- PHP驗(yàn)證碼類(lèi)代碼( 最新修改,完全定制化! )
- php 驗(yàn)證碼實(shí)例代碼
- PHP 動(dòng)態(tài)隨機(jī)生成驗(yàn)證碼類(lèi)代碼
- php 生成隨機(jī)驗(yàn)證碼圖片代碼
- php5 圖片驗(yàn)證碼實(shí)現(xiàn)代碼
- php 驗(yàn)證碼(傾斜,正弦干擾線(xiàn),黏貼,旋轉(zhuǎn))
相關(guān)文章
一個(gè)簡(jiǎn)單的php實(shí)現(xiàn)的MySQL數(shù)據(jù)瀏覽器
一個(gè)簡(jiǎn)單的php實(shí)現(xiàn)的MySQL數(shù)據(jù)瀏覽器...2007-03-03
IIS下配置頁(yè)面重寫(xiě)(配合插件url-rewrite2去除頁(yè)面后綴名)的實(shí)現(xiàn)方法
這篇文章主要介紹了IIS下配置頁(yè)面重寫(xiě)(配合插件url-rewrite2去除頁(yè)面后綴名)的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
CI框架驗(yàn)證碼CAPTCHA輔助函數(shù)用法實(shí)例
這篇文章主要介紹了CI框架驗(yàn)證碼CAPTCHA輔助函數(shù)用法,以實(shí)例形式詳細(xì)講述了基于CAPTCHA輔助函數(shù)實(shí)現(xiàn)驗(yàn)證碼的方法,需要的朋友可以參考下
2014-11-11 
