📄 seccode.class.php
字号:
<?php
/*
[Discuz!] (C)2001-2007 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: seccode.php 10799 2007-10-10 10:35:01Z monkey $
*/
class seccode {
var $code; //100000-999999 范围内随机
var $type = 0; //0 英文图片验证码 1 中文图片验证码 2 Flash 验证码 3 语音验证码
var $width = 0; //宽度
var $height = 0; //高度
var $background = 1; //随机图片背景
var $adulterate = 1; //随机背景图形
var $ttf = 0; //随机 TTF 字体
var $angle = 0; //随机倾斜度
var $color = 1; //随机颜色
var $size = 0; //随机大小
var $shadow = 1; //文字阴影
var $animator = 0; //GIF 动画
var $fontpath = ''; //TTF 字库目录
var $datapath = ''; //图片、声音、Flash 等数据目录
var $includepath= ''; //其它包含文件目录
var $fontcolor;
var $im;
function seccodeconvert(&$seccode) {
$s = sprintf('%04s', base_convert($seccode, 10, 20));
$seccodeunits = 'CEFHKLMNOPQRSTUVWXYZ';
$seccode = '';
for($i = 0; $i < 4; $i++) {
$unit = ord($s{$i});
$seccode .= ($unit >= 0x30 && $unit <= 0x39) ? $seccodeunits[$unit - 0x30] : $seccodeunits[$unit - 0x57];
}
}
function display() {
$this->type == 2 && !extension_loaded('ming') && $this->type = 0;
$this->width = $this->width >= 0 && $this->width <= 200 ? $this->width : 150;
$this->height = $this->height >= 0 && $this->height <= 80 ? $this->height : 60;
$this->seccodeconvert($this->code);
if($this->type < 2 && function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') &&
function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') &&
function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))) {
$this->image();
} elseif($this->type == 2 && extension_loaded('ming')) {
$this->flash();
} elseif($this->type == 3) {
$this->audio();
} else {
$this->bitmap();
}
}
function fileext($filename) {
return trim(substr(strrchr($filename, '.'), 1, 10));
}
function image() {
$bgcontent = $this->background();
if($this->animator == 1 && function_exists('imagegif')) {
include_once $this->includepath.'gifmerge.class.php';
$trueframe = mt_rand(1, 9);
for($i = 0; $i <= 9; $i++) {
$this->im = imagecreatefromstring($bgcontent);
$x[$i] = $y[$i] = 0;
$this->adulterate && $this->adulterate();
if($i == $trueframe) {
$this->ttf && function_exists('imagettftext') || $this->type == 1 ? $this->ttffont() : $this->giffont();
$d[$i] = mt_rand(250, 400);
} else {
$this->adulteratefont();
$d[$i] = mt_rand(5, 15);
}
ob_start();
imagegif($this->im);
imagedestroy($this->im);
$frame[$i] = ob_get_contents();
ob_end_clean();
}
$anim = new GifMerge($frame, 255, 255, 255, 0, $d, $x, $y, 'C_MEMORY');
header('Content-type: image/gif');
echo $anim->getAnimation();
} else {
$this->im = imagecreatefromstring($bgcontent);
$this->adulterate && $this->adulterate();
$this->ttf && function_exists('imagettftext') || $this->type == 1 ? $this->ttffont() : $this->giffont();
if(function_exists('imagepng')) {
header('Content-type: image/png');
imagepng($this->im);
} else {
header('Content-type: image/jpeg');
imagejpeg($this->im, '', 100);
}
imagedestroy($this->im);
}
}
function background() {
$this->im = imagecreatetruecolor($this->width, $this->height);
$backgroundcolor = imagecolorallocate($this->im, 255, 255, 255);
$backgrounds = $c = array();
if($this->background && function_exists('imagecreatefromjpeg') && function_exists('imagecolorat') && function_exists('imagecopymerge') &&
function_exists('imagesetpixel') && function_exists('imageSX') && function_exists('imageSY')) {
if($handle = @opendir($this->datapath.'background/')) {
while($bgfile = @readdir($handle)) {
if(preg_match('/\.jpg$/i', $bgfile)) {
$backgrounds[] = $this->datapath.'background/'.$bgfile;
}
}
@closedir($handle);
}
if($backgrounds) {
$imwm = imagecreatefromjpeg($backgrounds[array_rand($backgrounds)]);
$colorindex = imagecolorat($imwm, 0, 0);
$this->c = imagecolorsforindex($imwm, $colorindex);
$colorindex = imagecolorat($imwm, 1, 0);
imagesetpixel($imwm, 0, 0, $colorindex);
$c[0] = $c['red'];$c[1] = $c['green'];$c[2] = $c['blue'];
imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 200 - $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($imwm), 100);
imagedestroy($imwm);
}
}
if(!$this->background || !$backgrounds) {
for($i = 0;$i < 3;$i++) {
$start[$i] = mt_rand(200, 255);$end[$i] = mt_rand(100, 150);$step[$i] = ($end[$i] - $start[$i]) / $this->width;$c[$i] = $start[$i];
}
for($i = 0;$i < $this->width;$i++) {
$color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]);
imageline($this->im, $i, 0, $i-$angle, $this->height, $color);
$c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2];
}
$c[0] -= 20;$c[1] -= 20;$c[2] -= 20;
}
ob_start();
if(function_exists('imagepng')) {
imagepng($this->im);
} else {
imagejpeg($this->im, '', 100);
}
imagedestroy($this->im);
$bgcontent = ob_get_contents();
ob_end_clean();
$this->fontcolor = $c;
return $bgcontent;
}
function adulterate() {
$linenums = $this->height / 10;
for($i=0; $i <= $linenums; $i++) {
$color = $this->color ? imagecolorallocate($this->im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
$x = mt_rand(0, $this->width);
$y = mt_rand(0, $this->height);
if(mt_rand(0, 1)) {
imagearc($this->im, $x, $y, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, 360), mt_rand(0, 360), $color);
} else {
imageline($this->im, $x, $y, $linex + mt_rand(0, $linemaxlong), $liney + mt_rand(0, mt_rand($this->height, $this->width)), $color);
}
}
}
function adulteratefont() {
$seccodeunits = 'BCEFGHJKMPQRTVWXY2346789';
$x = $this->width / 4;
$y = $this->height / 10;
$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
for($i = 0; $i <= 3; $i++) {
$adulteratecode = $seccodeunits[mt_rand(0, 23)];
imagechar($this->im, 5, $x * $i + mt_rand(0, $x - 10), mt_rand($y, $this->height - 10 - $y), $adulteratecode, $text_color);
}
}
function ttffont() {
$seccode = $this->code;
$charset = $GLOBALS['charset'];
$seccoderoot = $this->type ? $this->fontpath.'ch/' : $this->fontpath.'en/';
$dirs = opendir($seccoderoot);
$seccodettf = array();
while($entry = readdir($dirs)) {
if($entry != '.' && $entry != '..' && in_array(strtolower($this->fileext($entry)), array('ttf', 'ttc'))) {
$seccodettf[] = $entry;
}
}
if(empty($seccodettf)) {
$this->giffont();
return;
}
$seccodelength = 4;
if($this->type && !empty($seccodettf)) {
if(strtoupper($charset) != 'UTF-8') {
include $this->includepath.'chinese.class.php';
$cvt = new Chinese($charset, 'utf8');
$seccode = $cvt->Convert($seccode);
}
$seccode = array(substr($seccode, 0, 3), substr($seccode, 3, 3));
$seccodelength = 2;
}
$widthtotal = 0;
for($i = 0; $i < $seccodelength; $i++) {
$font[$i]['font'] = $seccoderoot.$seccodettf[array_rand($seccodettf)];
$font[$i]['angle'] = $this->angle ? mt_rand(-30, 30) : 0;
$font[$i]['size'] = $this->type ? $this->width / 7 : $this->width / 6;
$this->size && $font[$i]['size'] = mt_rand($font[$i]['size'] - $this->width / 40, $font[$i]['size'] + $this->width / 20);
$box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]);
$font[$i]['zheight'] = max($box[1], $box[3]) - min($box[5], $box[7]);
$box = imagettfbbox($font[$i]['size'], $font[$i]['angle'], $font[$i]['font'], $seccode[$i]);
$font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]);
$font[$i]['hd'] = $font[$i]['height'] - $font[$i]['zheight'];
$font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this->width / 8);
$font[$i]['width'] = $font[$i]['width'] > $this->width / $seccodelength ? $this->width / $seccodelength : $font[$i]['width'];
$widthtotal += $font[$i]['width'];
}
$x = mt_rand($font[0]['angle'] > 0 ? cos(deg2rad(90 - $font[0]['angle'])) * $font[0]['zheight'] : 1, $this->width - $widthtotal);
!$this->color && $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
for($i = 0; $i < $seccodelength; $i++) {
if($this->color) {
$this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
$this->shadow && $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
} elseif($this->shadow) {
$text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -