<?
error_reporting(7);
session_start();
class image{
var $im;
var $im_width;
var $im_height;
var $rand_name = true ;//use rand file name for image
var $image_type = array(1=>"GIF",2=>"JPEG",3=>"PNG");
var $header = array(1=>"image/gif",2=>"image/jpeg",3=>"image/png");
var $ext = array(1=>"gif",2=>"jpg",3=>"png");
var $bgimage = "randpicbg.gif";
var $src_im;
var $src_imwidth;
var $src_imheight;
var $exten = '';
var $usettftext = false;
var $usetruecolor = false;
var $src_type;
var $use3D = true;
var $image;
var $ds_imwidth;
var $ds_imheight;
var $outto ;
var $newfile ;
var $filepre = "85518_";
var $defaultw ;
var $defaulth;
function show(){
$data = explode(",", $_GET['data']);
$tag = explode(",", $_GET['tag']);
$title = trim($title);
$width = $_GET['width'];
$height = $_GET['height'];
$radius = $_GET['radius'];
$radius2 = $_GET['radius2'];
if (isset($_GET['use3D'])) $this->use3D = $_GET['use3D'];
switch(trim($_GET['do'])) {
//显示扇形图
case "show_circle" :
$this->createCircle($data, $title , $radius, $radius2 , $width, $height , $tag );
break;
// 显示条形图
case "show_bar" :
$this->createBar($data, $title, $width, $height, $tag);
break;
// 产生随机图片
/**
* @ param integer length randcode maxlength
* @ param string text show text on randimage
* @ param integer height&width show image width and height
**/
case "randpic":
$length = @trim($_GET['length']);
$text = @trim($_GET['text']);
if (empty($text)) $text = $this->randcode($length, 'digit');
if ($_SESSION['randcode'] || $_SESSION['randcode'] == ""){
$_SESSION['randcode'] = $text;
} else {
$randcode = $text;
session_register("randcode");
}
$width = @trim($_GET['width']);
$height = @trim($_GET['height']);
if (!empty($width)&&!empty($height)){
if (!ereg("[0-9]{1,3}",$width)||!ereg("[0-9]{1,3}",$height))
$this->halt("提交的参数错误");
$this->create_randpic($text,$width,$height);
}else $this->create_randpic($text);
break;
//显示图片
/*
* @ param string src source image file
* @ param integer width show image width
* @ param integer height show image height
*/
case "spic":
$image = @$_GET['src'];
if (!$this->_check_image($image)){
$this->halt("提交图片错误");
}
$width = intval($_GET['width']);
$height = intval($_GET['height']);
$width = $width ? $width : $this->defaultw;
$height = $height ? $height : $this->defaulth;
if (!empty($width)&&!empty($height)){
if (!ereg("[0-9]{1,3}",$width)||!ereg("[0-9]{1,3}",$height))
$thispic->halt("提交的参数错误");
$this->ds_imwidth = $width;
$this->ds_imheight = $height;
$this->makescalepic($image);
}else{
$this->ds_imwidth = 300;
$this->ds_imheight =200;
$this->makescalepic($image);
}
break;
//缩放图片
/**
* @ param integer n zoom image to width*n and height *n
* @ param string src source image file
**/
case "zoom":
$image = @$_GET['src'];
if (!$this->_check_image($image)){
$this->halt("提交图片错误");
}
$n = @trim($_GET['n']);
if (!empty($n)){
if (!ereg("[0-9]{1,}",$n))
$this->halt("提交的参数错误");
$this->zoompic($image,intval($n));
}else $this->halt("参数错误");
}
}
/**
* @ param integer length length for randcode
* @ param string type of randcode , example : digit or char
* @ return string;
**/
function randcode($length = 8,$type = ''){
$code = '';
$randstr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randstr2 = '0123456789';
if ($type == '') $randorg = $randstr.$randstr2;
else if ($type == 'char')
$randorg = $randstr;
else $randorg = $randstr2;
for($i = 0; $i < $length;$i++){
$code .= substr($randorg,(rand(0,100)%strlen($randorg)),1);
}
return $code;
}
/**
* @ param integer n number of randcolor count;
* @ param integer c static variable , save number of count
* @ return RGB Color array
* @ access private
**/
function _randRGB(){
static $tempcolor = array();
static $i = 0 ;
$r = mt_rand(170,255);
$g = mt_rand(150, 250);
$b = mt_rand(120,250);
$t = $r.":".$g.":".$b;
if ($i < 100 ){
$tempcolor[$i] = $t;
$i += 1;
$this->_randRGB();
}
return $tempcolor;
}
/**
* @ param string type type of image
* @ param file image source image file handle
* @ access private
* @ return image handle
**/
function _create_image($type,$image){
switch($type){
case "GIF" :
if ($this->_testing_gif())
{
$tim = @imagecreatefromgif($image);
break;
}
case "PNG" :
$tim = @imagecreatefrompng($image);
break;
case "JPEG" :
$tim = imagecreatefromjpeg($image);
break;
}
return $tim;
}
function _locatecolor($im){
$this->white = imagecolorallocate($im,255,255,255);
$this->red = imagecolorallocate($im,255,0,0);
$this->blue = imagecolorallocate($im,0,0,255);
$this->brown = imagecolorallocate($im,100,0,0);
$this->gray = imagecolorallocate($im,149,149,149);
$this->black = imagecolorallocate($im,0,0,0);
$this->lightblue = imagecolorallocate($im, 165, 215, 216);
$this->middleblue = imagecolorallocate($im, 61, 182, 220);
$this->darkblue = imagecolorallocate($im, 0, 91, 136);
$this->middleorange = imagecolorallocate($im, 255, 153, 0);
$this->darkorange = imagecolorallocate($im, 255, 51, 51);
$this->lightorange = imagecolorallocate($im, 255, 230, 92);
}
function _create_name($ext,$name = ''){
if ($name == ''){
$iname = md5(uniqid(rand()));
$iname .= ".".$ext;
}
else $iname=$name.".".$ext;
return $iname;
}
function _rand_color(){
$color = array(0 => $this->red,1 => $this->blue,2 => $this->brown, 3 => $this->gray, 4 => $this->black, 5 => $this->lightblue, 6 => $this->middleblue, 7 => $this->darkblue, 8 => $this->lightorange, 9 => $this->middleorange, 10 => $this->darkorange);
return $color[mt_rand(0,count($color)-1)];
}
function _create_header($type){
return header("Content-Type:image/".strtolower($type));
}
function _testing_gif(){
if (!function_exists("imagegif"))
return false;
else return true;
}
function _check_image($image){
if (!@getimagesize($image))
return false;
else return true;
}
function _output($outto,$ds,$src_type,$newfile,$rand_name,$exten,$image,$filepre){
if ($outto == 'screen'){
$this->output($ds,$src_type);
}else{
if ($newfile){
if ($rand_name){
$name = $this->_create_name($exten);
$this->output($ds,$src_type,$name);
}else{
$filename = substr($image,0,strrpos($image,"."));
$name = $this->_create_name($exten,$filepre.$filename);
$this->output($ds,$src_type,$name);
}
}else{
$this->output($ds,$src_type,$image);
}
}
}
function _getimageinfo($image){
$info = getimagesize($image);
$this->src_type = $this->image_type[$info[2]];
$this->exten = $this->ext[$info[2]];
$this->src_imwidth = $info[0];
$this->src_imheight = $info[1];
}
function output($im,$type,$name = ''){
if ($name == ''){
switch($type){
case "GIF" :
if ($this->_testing_gif())
{
imagegif($im);
break;
}
case "PNG" :
imagepng($im);
break;
case "JPEG" :
imagejpeg($im);
}
}else {
switch($type){
case "GIF" :
if ($this->_testing_gif())
{
imagegif($im);
break;
}
case "PNG" :
imagepng($im);
break;
case "JPEG" :
imagejpeg($im);
}
}
}
function create_randpic($text,$width = 0,$height = 0){
$this->_create_header("PNG");
if ($width == 0) $width = strlen($text)*15+5;
if ($height == 0) $height = 23;
$this->_getimageinfo($this->bgimage);
$sim = $this->_create_image($this->src_type,$this->bgimage);
if (function_exists("imagecreatetruecolor") && $this->usetruecolor)
$this->im = imagecreatetruecolor($width,$height);
else $this->im = imagecreate($width, $height);
imagecopyresized($this->im,$sim,0,0,0,0,$width,$height,$this->src_imwidth,$this->src_imheight);
$this->_locatecolor($this->im);
imagefill($this->im,0,0,$this->white);
for($i = 0;$i < strlen($text);$i++){
if ($this->usettftext){
imagettftext($this->im,15,0,2+$i*15,18,$this->_rand_color(),"rand.TTF",$text[$i]);
}else{
imagechar($this->im,5,2+$i*15,1,$text[$i],$this->_rand_color());
}
}
$this->output($this->im,"PNG");
}
/**
* @ param array pointArray create rectanglebar data
* @ param string title the string show on the top of rectanglebar
* @ param integer width image height
* @ param integer height image height
* @ access public
* @ return showimage or save image to the file
**/
function createBar($pointArray, $title = "", $width, $height, $tag = ''){
$data = $pointArray;
$item = array();
$total = array_sum($data);
$space = 20;
$max = max($data);
$min = min($data);
$sum = 0;
for($i = 0; $i < count($data); $i++){
$item[$i]['data'] = $data[$i];
$item[$i]['scale'] = round($data[$i]/$total, 3) * 100 . "%";
$sum += $data[$i];
}
header("image/png");
$im = imagecreate($width, $height);
$this->_locatecolor($im);
$xleft = 5; $ytop = 5;$ybottom = 15;
$barheight = $height - $ybottom - $ytop - 30;
$per = $barheight / $sum;
$string = "Powered by 85518.net MAX:[time] $max MIN:[time] $min";
imagestring($im,3, $width - (strlen($string))*7 - 5, 5 , $string , $this->black);
imageline($im, $xleft, $ytop, $xleft, $height - $ybottom, $this->black);
$ytrace = intval(($height - $ytop*2 - $ybottom)/10);
for($i = 0; $i < 10; $i++){
$x1 = $xleft - 5;
$x2 = $xleft;
$y = $height - $ybottom - ($i+1) * $ytrace;
imageline($im, $x1, $y, $x2, $y, $this->black);
}
if (count($item) < 6)
$xtrace = 30;
else $xtrace = intval(($width - $xleft*3)/(count($item)*2));
$str = 0;
$k = 0;
if (!isset($_SESSION['COLOR_RND'])) {
$COLOR_RND_BAR = array();
session_register("COLOR_RND");
$_SESSION['COLOR_RND'] = $this->_randRGB();
}
for($i = 0; $i < count($item) * 2; $i++){
$y1 = $height - $ybottom;
$y2 = $height - 10 ;
$x = ($i + 1) * $xtrace;
imageline($im, $x, $y2, $x, $y1, $this->black);
$fontx = $x + intval($xtrace/2);
$fonty = $y1 + 3;
if ($i % 2 == 0){
imagestring($im, 3, $fontx, $fonty, ($str++), $this->black);
$rx1 = $x;
$ry1 = $y1 - $item[$k]['data'] * $per ;
$rx2 = $x + $xtrace;
$ry2 = $y1 - 1 ;
$cc = explode(":", $_SESSION['COLOR_RND'][$i]);
$r = $cc[0];
$g = $cc[1];
$b = $cc[2];
$locate = imagecolorallocate($im, $r, $g, $b);
imagerectangle($im, $rx1, $ry1, $rx2, $ry2, $this->white);
imagestring($im, 3, $rx1, $ry1 - 13, $item[$k++]['scale'], $this->black);
imagefilledrectangle($im, $rx1, $ry1, $rx2, $ry2, $locate);
}
}
imageline($im, $xleft, $height - $ybottom, $width - $xleft, $height - $ybottom, $this->black);
imagepng($im);
imagedestroy($im);
}
/**
* @ param int array data create circle data point
* @ param string title the string show on top of the image
* @ param int radius the radius for circle width
* @ param int radius2 the radius for circle height
* @ param int width width for image
* @ param int height height for iamge
* @ param string tag string for image show title
* @ access public
* @ return image handle
**/
function createCircle($data, $title = '', $radius, $radius2 = '', $width = '', $height = '', $tag = ''){
if ($radius2 == '') $radius2 = $radius;
if ($width == '') $width = $radius + 20;
if ($height == '') $height = $radius2 + 20;
$im = imagecreate($width , $height);
$this->_locatecolor($im);
$max = max($data); $min = min($data);
$total = array_sum($data);
$perpix = 360/$total;
$datashow = array();
for($i = 0; $i < count($data); $i++){
$datashow[$i]['data'] = $data[$i];
$datashow[$i]['scale'] = round($data[$i]/$total, 4) * 100 ."%";
}
$cx = intval($width/2);
$cy = intval($height/2);
$w = $radius;
$h = $radius2;
$start = 0;
if (!isset($_SESSION['COLOR_RND'])) {
$COLOR_RND = array();
session_register("COLOR_RND");
$_SESSION['COLOR_RND'] = $this->_randRGB();
}
for($i = 0; $i < count($datashow); $i++){
$s = $start;
$e = $s + $datashow[$i]['data'] * $perpix;
$start += $datashow[$i]['data'] * $perpix;
$cc = explode(":", $_SESSION['COLOR_RND'][$i]);
$r = $cc[0];
$g = $cc[1];
$b = $cc[2];
//use 3D effect
if ($this->use3D){
if ($s >= 0 and $s <= 180){
$dregress = imagecolorallocate($im, $r - 60, $g - 60, $b - 60);
if ($e <= 180){
for($j = 0; $j < 15; $j++){
imagearc($im, $cx, $cy + $j, $w, $h, $s, $e, $dregress);
}
}else{
for ($j = 0; $j < 15; $j++)
imagearc($im, $cx, $cy + $j, $w, $h, $s, 180, $dregress);
}
}
}
$color = imagecolorallocate($im, $r, $g, $b);
imagefilledarc($im, $cx, $cy, $w, $h, $s, $e, $color, IMG_ARC_PIE);
$c = ($e + $s) / 2 ;
//另外一个条件 x^2/a^2 + y^2/b^2 = 1; $y/$x = tan($c);
$sx = $cx + $w * 0.7 * cos(deg2rad($c));
$sy = $cy + $h * 0.7 * sin(deg2rad($c));
imagestring($im, 3, $sx, $sy, $datashow[$i]['scale'], $this->black);
}
header("image/gif");
imagegif($im);
imagedestroy($im);
}
function imagescale($width = 88,$height = 88, $output = 'screen',$rand_name = false,$newfile = true){
$this->ds_imwidth = $width;
$this->ds_imheight = $height;
$this->rand_name = $rand_name;
$this->outto = $output;
$this->newfile =$newfile;
}
function _makescale(){
$scale = min($this->ds_imwidth/$this->src_imwidth,$this->ds_imheight/$this->src_imheight);
return $scale;
}
/**
* @ param string image source image file
* @ access public
* @ return scale image
**/
function makescalepic($image){
if (!$this->_check_image($image))
$this->halt('Open the image file error');
$this->image = $image;
$this->_getimageinfo($this->image);
$scale = $this->_makescale();
$this->im = $this->_create_image($this->src_type,$this->image);
$new_width = intval($this->src_imwidth * $scale);
$new_height = intval($this->src_imheight * $scale);
if (!function_exists('imagecreatetruecolor'))
$this->halt("Sorry,your system cann't support this function,please upgrade your GD Version");
$ds = imagecreatetruecolor($new_width,$new_height);
@imagecopyresized($ds,$this->im,0,0,0,0,$new_width,$new_height,$this->src_imwidth,$this->src_imheight);
$this->_output($this->outto,$ds,strtoupper($this->src_type),$this->newfile,$this->rand_name,$this->exten,$this->image,$this->filepre);
}
/**
* @ param string image source image file
* @ param int width destination width for image file
* @ param int height destination height for image file
* @ param string destination filename
* @ param string path destination filepath
* @ param string watermark
* @ return string filename
*/
function createscaleimage($image, $width, $height, $dstimage = "", $path = "", $watermark = ""){
if ($dstimage == ""){
$filename = ex."_".md5(uniqid(microtime())).".jpg";
} else {
$filename = $dstimage;
}
if (!$this->_check_image($image))
$this->halt('Open the image file error');
$this->ds_imwidth = $width;
$this->ds_imheight = $height;
$this->image = $image;
$this->_getimageinfo($this->image);
$scale = $this->_makescale();
$this->im = $this->_create_image($this->src_type,$this->image);
$new_width = intval($this->src_imwidth * $scale);
$new_height = intval($this->src_imheight * $scale);
if (!function_exists('imagecreatetruecolor'))
$this->halt("Sorry,your system cann't support this function,please upgrade your GD Version");
$ds = imagecreatetruecolor($new_width,$new_height);
imagecopyresized($ds,$this->im,0,0,0,0,$new_width,$new_height,$this->src_imwidth,$this->src_imheight);
imagejpeg($ds, $path."/".$filename);
return $path."/".$filename;
}
/**
* @ param string image source image file
* @ param integer n zoom to n * width and n * height
* @ access public
* @ return image file to screen
**/
function zoompic($image,$n){
if (!$this->_check_image($image))
$this->halt('Open the image file error');
$this->image = $image;
$this->_getimageinfo($this->image);
$this->im = $this->_create_image($this->src_type,$this->image);
$new_width = $this->src_imwidth * $n;
$new_height = $this->src_imheight * $n;
if (!function_exists('imagecreatetruecolor'))
$this->halt("Sorry,your system cann't support this function,please upgrade your GD Version");
$ds = imagecreatetruecolor($new_width,$new_height);
imagecopyresized($ds,$this->im,0,0,0,0,$new_width,$new_height,$this->src_imwidth,$this->src_imheight);
$this->_output($this->outto,$ds,strtoupper($this->src_type),$this->newfile,$this->rand_name,$this->exten,$this->image,$this->filepre);
}
function halt($err){
echo $err;
exit();
}
}
?>
http://www.wujianrong.com/mt-tb.cgi/5832