📄 jpgraph.php
字号:
function Raise($aMsg,$aHalt=true) { $img_iconerror = 'iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAAAaV'. 'BMVEX//////2Xy8mLl5V/Z2VvMzFi/v1WyslKlpU+ZmUyMjEh/'. 'f0VyckJlZT9YWDxMTDjAwMDy8sLl5bnY2K/MzKW/v5yyspKlpY'. 'iYmH+MjHY/PzV/f2xycmJlZVlZWU9MTEXY2Ms/PzwyMjLFTjea'. 'AAAAAXRSTlMAQObYZgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAACx'. 'IAAAsSAdLdfvwAAAAHdElNRQfTBgISOCqusfs5AAABLUlEQVR4'. '2tWV3XKCMBBGWfkranCIVClKLd/7P2Q3QsgCxjDTq+6FE2cPH+'. 'xJ0Ogn2lQbsT+Wrs+buAZAV4W5T6Bs0YXBBwpKgEuIu+JERAX6'. 'wM2rHjmDdEITmsQEEmWADgZm6rAjhXsoMGY9B/NZBwJzBvn+e3'. 'wHntCAJdGu9SviwIwoZVDxPB9+Rc0TSEbQr0j3SA1gwdSn6Db0'. '6Tm1KfV6yzWGQO7zdpvyKLKBDmRFjzeB3LYgK7r6A/noDAfjtS'. 'IXaIzbJSv6WgUebTMV4EoRB8a2mQiQjgtF91HdKDKZ1gtFtQjk'. 'YcWaR5OKOhkYt+ZsTFdJRfPAApOpQYJTNHvCRSJR6SJngQadfc'. 'vd69OLMddVOPCGVnmrFD8bVYd3JXfxXPtLR/+mtv59/ALWiiMx'. 'qL72fwAAAABJRU5ErkJggg==' ; if( headers_sent() ) { // Special case for headers already sent error. Dont // return an image since it can't be displayed die("<b>JpGraph Error:</b> ".$aMsg); } // Create the error icon GD $erricon = imagecreatefromstring(base64_decode($img_iconerror)); // Create an image that contains the error text. $w=370; $h=100; $img = new Image($w,$h); // Drop shadow $img->SetColor("gray"); $img->FilledRectangle(5,5,$w-1,$h-1,10); $img->SetColor("gray:0.7"); $img->FilledRectangle(5,5,$w-3,$h-3,10); // Window background $img->SetColor("lightblue"); $img->FilledRectangle(1,1,$w-5,$h-5); $img->CopyCanvasH($img->img,$erricon,5,30,0,0,40,40); // Window border $img->SetColor("black"); $img->Rectangle(1,1,$w-5,$h-5); $img->Rectangle(0,0,$w-4,$h-4); // Window top row $img->SetColor("darkred"); for($y=3; $y < 18; $y += 2 ) $img->Line(1,$y,$w-25,$y); $img->Line(1,17,$w-5,17); $img->Line($w-25,1,$w-25,16); // "Close button" $img->Line($w-18,6,$w-12,12); $img->Line($w-18,7,$w-12,13); $img->Line($w-18,12,$w-12,6); $img->Line($w-18,13,$w-12,7); // "White shadow" $img->SetColor("white"); // "Button" $img->Line($w-24,3,$w-24,15); $img->Line($w-24,2,$w-7,2); // Left window edge $img->Line(2,2,2,$h-5); $img->Line(2,2,$w-27,2); // "Gray button shadow" $img->SetColor("darkgray"); $img->Line($w-6,3,$w-6,16); $img->Line($w-24,16,$w-7,16); // Gray window shadow $img->Line(2,$h-6,$w-5,$h-6); $img->Line(3,$h-7,$w-5,$h-7); // Window title $m = floor($w/2-5); $l = 100; $img->SetColor("lightgray:1.3"); $img->FilledRectangle($m-$l,2,$m+$l,16); // Stroke text $img->SetColor("darkred"); $img->SetFont(FF_FONT2,FS_BOLD); $img->StrokeText($m-50,15,"JpGraph Error"); $img->SetColor("black"); $img->SetFont(FF_FONT1,FS_NORMAL); $txt = new Text(wordwrap($aMsg,52),52,25); $txt->Align("left","top"); $txt->Stroke($img); $img->Headers(); $img->Stream(); die(); }}//// A wrapper class that is used to access the specified error object// (to hide the global error parameter and avoid having a GLOBAL directive// in all methods.//class JpGraphError { function Install($aErrObject) { GLOBAL $__jpg_err; $__jpg_err = $aErrObject; } function Raise($aMsg,$aHalt=true){ GLOBAL $__jpg_err; $tmp = new $__jpg_err; $tmp->Raise($aMsg,$aHalt); }}//// ... and install the default error handler//if( USE_IMAGE_ERROR_HANDLER ) { JpGraphError::Install("JpGraphErrObjectImg");}else { JpGraphError::Install("JpGraphErrObject");}//// Setup PHP error handler//error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);function _phpErrorHandler($errno,$errmsg,$filename, $linenum, $vars) { JpGraphError::Raise('In '.basename($filename).'#'.$linenum."\n".$errmsg);}#set_error_handler("_phpErrorHandler");////Check if there were any warnings, perhaps some wrong includes by the//user//if( isset($GLOBALS['php_errormsg']) ) { JpGraphError::Raise("<b>General PHP error:</b><br>".$GLOBALS['php_errormsg']);}//// Routine to determine if GD1 or GD2 is installed//function CheckGDVersion() { ob_start(); phpinfo(8); // Just get the modules loaded $a = ob_get_contents(); ob_end_clean(); if( preg_match('/.*GD Version.*(1\.).*/',$a,$m) ) { $r=1;$v=$m[1]; } elseif( preg_match('/.*GD Version.*(2\.).*/',$a,$m) ) { $r=2;$v=$m[1]; } else { $r=0;$v=$m[1]; } return $r;}//// Check what version of the GD library is installed.//if( USE_LIBRARY_GD2 === 'auto' ) { $gdversion = CheckGDVersion(); if( $gdversion == 2 ) { $GLOBALS['gd2'] = true; $GLOBALS['copyfunc'] = 'imagecopyresampled'; } elseif( $gdversion == 1 ) { $GLOBALS['gd2'] = false; $GLOBALS['copyfunc'] = 'imagecopyresized'; } else { JpGraphError::Raise(" Your PHP installation does not seem to have the required GD library. Please see the PHP documentation on how to install and enable the GD library."); }}else { $GLOBALS['gd2'] = USE_LIBRARY_GD2; $GLOBALS['copyfunc'] = USE_LIBRARY_GD2 ? 'imagecopyresampled' : 'imagecopyresized';}// Usefull mathematical functionfunction sign($a) {return $a >= 0 ? 1 : -1;}// Utility function to generate an image name based on the filename we// are running from and assuming we use auto detection of graphic format// (top level), i.e it is safe to call this function// from a script that uses JpGraphfunction GenImgName() { global $_SERVER; $supported = imagetypes(); if( $supported & IMG_PNG ) $img_format="png"; elseif( $supported & IMG_GIF ) $img_format="gif"; elseif( $supported & IMG_JPG ) $img_format="jpeg"; if( !isset($_SERVER['PHP_SELF']) ) JpGraphError::Raise(" Can't access PHP_SELF, PHP global variable. You can't run PHP from command line if you want to use the 'auto' naming of cache or image files."); $fname=basename($_SERVER['PHP_SELF']); // Replace the ".php" extension with the image format extension return substr($fname,0,strlen($fname)-4).".".$img_format;}class LanguageConv { var $g2312 = null ; function Convert($aTxt,$aFF) { if( LANGUAGE_CYRILLIC ) { if( CYRILLIC_FROM_WINDOWS ) { $aTxt = convert_cyr_string($aTxt, "w", "k"); } $isostring = convert_cyr_string($aTxt, "k", "i"); $unistring = LanguageConv::iso2uni($isostring); return $unistring; } elseif( $aFF === FF_SIMSUN ) { // Do Chinese conversion if( $this->g2312 == null ) { include_once 'jpgraph_gb2312.php' ; $this->g2312 = new GB2312toUTF8(); } return $this->g2312->gb2utf8($aTxt); } else return $aTxt; } // Translate iso encoding to unicode function iso2uni ($isoline){ for ($i=0; $i < strlen($isoline); $i++){ $thischar=substr($isoline,$i,1); $charcode=ord($thischar); $uniline.=($charcode>175) ? "&#" . (1040+($charcode-176)). ";" : $thischar; } return $uniline; }}//===================================================// CLASS JpgTimer// Description: General timing utility class to handle// timne measurement of generating graphs. Multiple// timers can be started by pushing new on a stack.//===================================================class JpgTimer { var $start; var $idx; //---------------// CONSTRUCTOR function JpgTimer() { $this->idx=0; }//---------------// PUBLIC METHODS // Push a new timer start on stack function Push() { list($ms,$s)=explode(" ",microtime()); $this->start[$this->idx++]=floor($ms*1000) + 1000*$s; } // Pop the latest timer start and return the diff with the // current time function Pop() { assert($this->idx>0); list($ms,$s)=explode(" ",microtime()); $etime=floor($ms*1000) + (1000*$s); $this->idx--; return $etime-$this->start[$this->idx]; }} // Class$gJpgBrandTiming = BRAND_TIMING;//===================================================// CLASS DateLocale// Description: Hold localized text used in dates// ToDOo: Rewrite this to use the real local locale// instead.//===================================================class DateLocale { var $iLocale = 'C'; // environmental locale be used by default var $iDayAbb = null; var $iShortDay = null; var $iShortMonth = null; var $iMonthName = null;//---------------// CONSTRUCTOR function DateLocale() { settype($this->iDayAbb, 'array'); settype($this->iShortDay, 'array'); settype($this->iShortMonth, 'array'); settype($this->iMonthName, 'array'); $this->Set('C'); }//---------------// PUBLIC METHODS function Set($aLocale) { if ( in_array($aLocale, array_keys($this->iDayAbb)) ){ $this->iLocale = $aLocale; return TRUE; // already cached nothing else to do! } $pLocale = setlocale(LC_TIME, 0); // get current locale for LC_TIME $res = setlocale(LC_TIME, $aLocale); if ( ! $res ){ JpGraphError::Raise("You are trying to use the locale ($aLocale) which your PHP installation does not support. Hint: Use '' to indicate the default locale for this geographic region."); return FALSE; } $this->iLocale = $aLocale; for ( $i = 0, $ofs = 0 - strftime('%w'); $i < 7; $i++, $ofs++ ){ $day = strftime('%a', strtotime("$ofs day")); $day{0} = strtoupper($day{0}); $this->iDayAbb[$aLocale][]= $day{0}; $this->iShortDay[$aLocale][]= $day; } for($i=1; $i<=12; ++$i) { list($short ,$full) = explode('|', strftime("%b|%B",strtotime("2001-$i-01"))); $this->iShortMonth[$aLocale][] = ucfirst($short); $this->iMonthName [$aLocale][] = ucfirst($full); } setlocale(LC_TIME, $pLocale); return TRUE; } function GetDayAbb() { return $this->iDayAbb[$this->iLocale]; } function GetShortDay() { return $this->iShortDay[$this->iLocale]; } function GetShortMonth() { return $this->iShortMonth[$this->iLocale]; } function GetShortMonthName($aNbr) { return $this->iShortMonth[$this->iLocale][$aNbr]; } function GetLongMonthName($aNbr) { return $this->iMonthName[$this->iLocale][$aNbr]; } function GetMonth() { return $this->iMonthName[$this->iLocale]; }}$gDateLocale = new DateLocale();$gJpgDateLocale = new DateLocale();//===================================================// CLASS FuncGenerator// Description: Utility class to help generate data for function plots. // The class supports both parametric and regular functions.//===================================================class FuncGenerator { var $iFunc='',$iXFunc='',$iMin,$iMax,$iStepSize; function FuncGenerator($aFunc,$aXFunc='') { $this->iFunc = $aFunc; $this->iXFunc = $aXFunc; } function E($aXMin,$aXMax,$aSteps=50) { $this->iMin = $aXMin; $this->iMax = $aXMax; $this->iStepSize = ($aXMax-$aXMin)/$aSteps; if( $this->iXFunc != '' ) $t = 'for($i='.$aXMin.'; $i<='.$aXMax.'; $i += '.$this->iStepSize.') {$ya[]='.$this->iFunc.';$xa[]='.$this->iXFunc.';}'; elseif( $this->iFunc != '' ) $t = 'for($x='.$aXMin.'; $x<='.$aXMax.'; $x += '.$this->iStepSize.') {$ya[]='.$this->iFunc.';$xa[]=$x;} $x='.$aXMax.';$ya[]='.$this->iFunc.';$xa[]=$x;'; else JpGraphError::Raise('FuncGenerator : No function specified. '); @eval($t); // If there is an error in the function specifcation this is the only // way we can discover that.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -