image.helper.php,image.php
/**
* 圖像操作類
*/
class helper_image
{
/**
* 生成圖像驗證碼
* @static
* @access public
* @param string $length 位數(shù)
* @param string $mode 類型
* @param string $type 圖像格式
* @param string $width 寬度
* @param string $height 高度
* @return string
*/
static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify') {
ob_clean();
$randval = helper_string::randString($length, $mode);
$session = Yaf_Session::getInstance();
$tmp = strtoupper($randval);
$session->$verifyName = $tmp;
$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
$im = imagecreatetruecolor($width, $height);
} else {
$im = imagecreate($width, $height);
}
$r = Array(225, 255, 255, 223);
$g = Array(225, 236, 237, 255);
$b = Array(225, 236, 166, 125);
$key = mt_rand(0, 3);
$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(隨機)
$borderColor = imagecolorallocate($im, 100, 100, 100); //邊框色
imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
// 干擾
for ($i = 0; $i < 10; $i++) {
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
}
for ($i = 0; $i < 25; $i++) {
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
}
for ($i = 0; $i < $length; $i++) {
imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
}
helper_image::output($im, $type);
}
static function output($im, $type='png', $filename='') {
header("Content-type: image/" . $type);
$ImageFun = 'image' . $type;
if (empty($filename)) {
$ImageFun($im);
} else {
$ImageFun($im, $filename);
}
imagedestroy($im);
}
static function echo_validate_image( $config = array(), $verifyName)
{
$config = array(
'font_size' => 14,
'img_height' => 24,
'word_type' => 2, // 1:數(shù)字 2:英文
'img_width' => 68,
'use_boder' => TRUE,
'font_file' => ROOT_PATH.'/public/admin/image/ggbi.ttf',
'filter_type' => 5);
//主要參數(shù)
$font_size = isset($config['font_size']) ? $config['font_size'] : 14;
$img_height = isset($config['img_height']) ? $config['img_height'] : 24;
$img_width = isset($config['img_width']) ? $config['img_width'] : 68;
$font_file = isset($config['font_file']) ? $config['font_file'] : ROOT_PATH.'/public/admin/image/ggbi.ttf';
$use_boder = isset($config['use_boder']) ? $config['use_boder'] : TRUE;
$filter_type = isset($config['filter_type']) ? $config['filter_type'] : 0;
//創(chuàng)建圖片,并設(shè)置背景色
$im = @imagecreate($img_width, $img_height);
imagecolorallocate($im, 255,255,255);
//文字隨機顏色
$fontColor[] = imagecolorallocate($im, 0x15, 0x15, 0x15);
$fontColor[] = imagecolorallocate($im, 0x95, 0x1e, 0x04);
$fontColor[] = imagecolorallocate($im, 0x93, 0x14, 0xa9);
$fontColor[] = imagecolorallocate($im, 0x12, 0x81, 0x0a);
$fontColor[] = imagecolorallocate($im, 0x06, 0x3a, 0xd5);
//獲取隨機字符
$rndstring = '';
for($i=0; $i<4; $i++)
{
if ($config['word_type'] == 1)
{
$c = chr(mt_rand(48, 57));
} else if($config['word_type'] == 2)
{
$c = chr(mt_rand(65, 90));
if( $c=='I' ) $c = 'P';
if( $c=='O' ) $c = 'N';
}
$rndstring .= $c;
}
//$_SESSION['securimage_code_value'] = strtolower($rndstring);
$session = Yaf_Session::getInstance();
$tmp = strtoupper($rndstring);
$session->$verifyName = $tmp;
$rndcodelen = strlen($rndstring);
//背景橫線
$lineColor1 = imagecolorallocate($im, 0xda, 0xd9, 0xd1);
for($j=3; $j<=$img_height-3; $j=$j+3)
{
imageline($im, 2, $j, $img_width - 2, $j, $lineColor1);
}
//背景豎線
$lineColor2 = imagecolorallocate($im, 0xda,0xd9,0xd1);
for($j=2;$j<100;$j=$j+6)
{
imageline($im, $j, 0, $j+8, $img_height, $lineColor2);
}
//畫邊框
if( $use_boder && $filter_type == 0 )
{
$bordercolor = imagecolorallocate($im, 0x9d, 0x9e, 0x96);
imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $bordercolor);
}
//輸出文字
$lastc = '';
for($i=0;$i
{
$bc = mt_rand(0, 1);
$rndstring[$i] = strtoupper($rndstring[$i]);
$c_fontColor = $fontColor[mt_rand(0,4)];
$y_pos = $i==0 ? 4 : $i*($font_size+2);
$c = mt_rand(0, 15);
@imagettftext($im, $font_size, $c, $y_pos, 19, $c_fontColor, $font_file, $rndstring[$i]);
$lastc = $rndstring[$i];
}
//圖象效果
switch($filter_type)
{
case 1:
imagefilter ( $im, IMG_FILTER_NEGATE);
break;
case 2:
imagefilter ( $im, IMG_FILTER_EMBOSS);
break;
case 3:
imagefilter ( $im, IMG_FILTER_EDGEDETECT);
break;
default:
break;
}
header("Pragma:no-cache\r\n");
header("Cache-Control:no-cache\r\n");
header("Expires:0\r\n");
//輸出特定類型的圖片格式,優(yōu)先級為 gif -> jpg ->png
//dump(function_exists("imagejpeg"));
if(function_exists("imagejpeg"))
{
header("content-type:image/jpeg\r\n");
imagejpeg($im);
}
else
{
header("content-type:image/png\r\n");
imagepng($im);
}
imagedestroy($im);
exit();
}
}
?>
一鍵復(fù)制
編輯
Web IDE
原始數(shù)據(jù)
按行查看
歷史
總結(jié)
以上是生活随笔為你收集整理的image.helper.php,image.php的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php长脚本,长PHP脚本运行多次
- 下一篇: php上传文件自动删除,jsp-解决文件