Upgrade jpgrah to 4.3.4
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Nigel Sheldon 2021-01-03 17:10:26 +01:00
commit b5868f05f6
72 changed files with 19157 additions and 10327 deletions

View file

@ -1,13 +1,13 @@
<?php
/*=======================================================================
// File: JPGRAPH_MGRAPH.PHP
// Description: Class to handle multiple graphs in the same image
// Created: 2006-01-15
// Ver: $Id: jpgraph_mgraph.php 781 2006-10-08 08:07:47Z ljp $
//
// Copyright (c) Aditus Consulting. All rights reserved.
//========================================================================
*/
// File: JPGRAPH_MGRAPH.PHP
// Description: Class to handle multiple graphs in the same image
// Created: 2006-01-15
// Ver: $Id: jpgraph_mgraph.php 1770 2009-08-17 06:10:22Z ljp $
//
// Copyright (c) Asial Corporation. All rights reserved.
//========================================================================
*/
//=============================================================================
// CLASS MGraph
@ -15,21 +15,28 @@
//=============================================================================
class MGraph
{
public $title = null;
public $subtitle = null;
public $subsubtitle = null;
protected $img=null;
protected $iCnt=0;
protected $iGraphs = array(); // image_handle, x, y, fx, fy, sizex, sizey
protected $iFillColor='white';
protected $iCurrentColor=0;
protected $lm=0;
protected $rm=0;
protected $tm=0;
protected $bm=0;
protected $lm=4;
protected $rm=4;
protected $tm=4;
protected $bm=4;
protected $iDoFrame = false;
protected $iFrameColor = 'black';
protected $iFrameWeight = 1;
protected $iLineWeight = 1;
protected $expired=false;
protected $img_format='png';
protected $cache=null;
protected $cache_name = '';
protected $inline=true;
protected $image_format='png';
protected $image_quality=75;
protected $iWidth=null;
protected $iHeight=null;
@ -39,12 +46,50 @@ class MGraph
protected $background_image_mix=100;
protected $background_image_y=null;
protected $background_image_x=null;
private $doshadow=false;
private $shadow_width=4;
private $shadow_color='gray@0.5';
public $footer;
// Create a new instane of the combined graph
public function MGraph($aWidth=null, $aHeight=null)
public function __construct($aWidth=null, $aHeight=null, $aCachedName='', $aTimeOut=0, $aInline=true)
{
$this->iWidth = $aWidth;
$this->iHeight = $aHeight;
// If the cached version exist just read it directly from the
// cache, stream it back to browser and exit
if ($aCachedName!='' && READ_CACHE && $aInline) {
$this->cache = new ImgStreamCache();
$this->cache->SetTimeOut($aTimeOut);
$image = new Image();
if ($this->cache->GetAndStream($image, $aCachedName)) {
exit();
}
}
$this->inline = $aInline;
$this->cache_name = $aCachedName;
$this->title = new Text();
$this->title->ParagraphAlign('center');
$this->title->SetFont(FF_FONT2, FS_BOLD);
$this->title->SetMargin(3);
$this->title->SetAlign('center');
$this->subtitle = new Text();
$this->subtitle->ParagraphAlign('center');
$this->subtitle->SetFont(FF_FONT1, FS_BOLD);
$this->subtitle->SetMargin(3);
$this->subtitle->SetAlign('center');
$this->subsubtitle = new Text();
$this->subsubtitle->ParagraphAlign('center');
$this->subsubtitle->SetFont(FF_FONT1, FS_NORMAL);
$this->subsubtitle->SetMargin(3);
$this->subsubtitle->SetAlign('center');
$this->footer = new Footer();
}
// Specify background fill color for the combined graph
@ -75,10 +120,6 @@ class MGraph
$aCenter=true;
$aX=null;
if ($GLOBALS['gd2'] && !USE_TRUECOLOR) {
JpGraphError::RaiseL(12001);
//("You are using GD 2.x and are trying to use a background images on a non truecolor image. To use background images with GD 2.x you <b>must</b> enable truecolor by setting the USE_TRUECOLOR constant to TRUE. Due to a bug in GD 2.0.1 using any truetype fonts with truecolor images will result in very poor quality fonts.");
}
if (is_numeric($aCenter_aX)) {
$aX=$aCenter_aX;
}
@ -89,7 +130,7 @@ class MGraph
JpGraphError::RaiseL(12002, $aFileName);
//('Incorrect file name for MGraph::SetBackgroundImage() : '.$aFileName.' Must have a valid image extension (jpg,gif,png) when using autodetection of image type');
}
$valid_formats = array('png', 'jpg', 'gif');
$aImgFormat = strtolower($e[count($e)-1]);
if ($aImgFormat == 'jpeg') {
@ -106,62 +147,27 @@ class MGraph
$this->background_image_y = $aY;
}
// Private helper function for backgound image
public function _loadBkgImage($aFile='')
{
if ($aFile == '') {
$aFile = $this->background_image;
}
// Remove case sensitivity and setup appropriate function to create image
// Get file extension. This should be the LAST '.' separated part of the filename
$e = explode('.', $aFile);
$ext = strtolower($e[count($e)-1]);
if ($ext == "jpeg") {
$ext = "jpg";
}
if (trim($ext) == '') {
$ext = 'png';
} // Assume PNG if no extension specified
$supported = imagetypes();
if (($ext == 'jpg' && !($supported & IMG_JPG)) ||
($ext == 'gif' && !($supported & IMG_GIF)) ||
($ext == 'png' && !($supported & IMG_PNG))) {
JpGraphError::RaiseL(12004, $aFile);//('The image format of your background image ('.$aFile.') is not supported in your system configuration. ');
}
if ($ext == "jpg" || $ext == "jpeg") {
$f = "imagecreatefromjpeg";
$ext = "jpg";
} else {
$f = "imagecreatefrom".$ext;
}
$img = @$f($aFile);
if (!$img) {
JpGraphError::RaiseL(12005, $aFile);
//(" Can't read background image: '".$aFile."'");
}
return $img;
}
public function _strokeBackgroundImage()
{
if ($this->background_image == '') {
return;
}
$bkgimg = $this->_loadBkgImage();
$bkgimg = Graph::LoadBkgImage('', $this->background_image);
// Background width & Heoght
$bw = imagesx($bkgimg);
$bh = imagesy($bkgimg);
// Canvas width and height
$cw = imagesx($this->img);
$ch = imagesy($this->img);
if ($this->doshadow) {
$cw -= $this->shadow_width;
$ch -= $this->shadow_width;
}
if ($this->background_image_x === null || $this->background_image_y === null) {
if ($this->background_image_center) {
// Center original image in the plot area
@ -176,87 +182,14 @@ class MGraph
$x = $this->background_image_x;
$y = $this->background_image_y;
}
$this->_imageCp($bkgimg, $x, $y, 0, 0, $bw, $bh, $this->background_image_mix);
}
public function _imageCp($aSrcImg, $x, $y, $fx, $fy, $w, $h, $mix=100)
{
imagecopymerge($this->img, $aSrcImg, $x, $y, $fx, $fy, $w, $h, $mix);
}
public function _imageCreate($aWidth, $aHeight)
{
if ($aWidth <= 1 || $aHeight <= 1) {
JpGraphError::RaiseL(12006, $aWidth, $aHeight);
//("Illegal sizes specified for width or height when creating an image, (width=$aWidth, height=$aHeight)");
}
if (@$GLOBALS['gd2']==true && USE_TRUECOLOR) {
$this->img = @imagecreatetruecolor($aWidth, $aHeight);
if ($this->img < 1) {
JpGraphError::RaiseL(12011);
// die("<b>JpGraph Error:</b> Can't create truecolor image. Check that you really have GD2 library installed.");
}
ImageAlphaBlending($this->img, true);
} else {
$this->img = @imagecreate($aWidth, $aHeight);
if ($this->img < 1) {
JpGraphError::RaiseL(12012);
// die("<b>JpGraph Error:</b> Can't create image. Check that you really have the GD library installed.");
}
}
}
public function _polygon($p, $closed=false)
{
if ($this->iLineWeight==0) {
return;
}
$n=count($p);
$oldx = $p[0];
$oldy = $p[1];
for ($i=2; $i < $n; $i+=2) {
imageline($this->img, $oldx, $oldy, $p[$i], $p[$i+1], $this->iCurrentColor);
$oldx = $p[$i];
$oldy = $p[$i+1];
}
if ($closed) {
imageline($this->img, $p[$n*2-2], $p[$n*2-1], $p[0], $p[1], $this->iCurrentColor);
}
}
public function _filledPolygon($pts)
{
$n=count($pts);
for ($i=0; $i < $n; ++$i) {
$pts[$i] = round($pts[$i]);
}
imagefilledpolygon($this->img, $pts, count($pts)/2, $this->iCurrentColor);
}
public function _rectangle($xl, $yu, $xr, $yl)
{
for ($i=0; $i < $this->iLineWeight; ++$i) {
$this->_polygon(array($xl+$i,$yu+$i,$xr-$i,$yu+$i,
$xr-$i,$yl-$i,$xl+$i,$yl-$i,
$xl+$i,$yu+$i));
}
}
public function _filledRectangle($xl, $yu, $xr, $yl)
{
$this->_filledPolygon(array($xl,$yu,$xr,$yu,$xr,$yl,$xl,$yl));
}
public function _setColor($aColor)
{
$this->iCurrentColor = $this->iRGB->Allocate($aColor);
imagecopymerge($this->img, $bkgimg, $x, $y, 0, 0, $bw, $bh, $this->background_image_mix);
}
public function AddMix($aGraph, $x=0, $y=0, $mix=100, $fx=0, $fy=0, $w=0, $h=0)
{
$this->_gdImgHandle($aGraph->Stroke(_IMG_HANDLER), $x, $y, $fx=0, $fy=0, $w, $h, $mix);
}
public function Add($aGraph, $x=0, $y=0, $fx=0, $fy=0, $w=0, $h=0)
{
$this->_gdImgHandle($aGraph->Stroke(_IMG_HANDLER), $x, $y, $fx=0, $fy=0, $w, $h);
@ -291,90 +224,72 @@ class MGraph
$this->expired = $aFlg;
}
// Generate image header
public function Headers()
{
// In case we are running from the command line with the client version of
// PHP we can't send any headers.
$sapi = php_sapi_name();
if ($sapi == 'cli') {
return;
}
if (headers_sent()) {
echo "<table border=1><tr><td><font color=darkred size=4><b>JpGraph Error:</b>
HTTP headers have already been sent.</font></td></tr><tr><td><b>Explanation:</b><br>HTTP headers have already been sent back to the browser indicating the data as text before the library got a chance to send it's image HTTP header to this browser. This makes it impossible for the library to send back image data to the browser (since that would be interpretated as text by the browser and show up as junk text).<p>Most likely you have some text in your script before the call to <i>Graph::Stroke()</i>. If this texts gets sent back to the browser the browser will assume that all data is plain text. Look for any text, even spaces and newlines, that might have been sent back to the browser. <p>For example it is a common mistake to leave a blank line before the opening \"<b>&lt;?php</b>\".</td></tr></table>";
die();
}
if ($this->expired) {
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
}
header("Content-type: image/$this->img_format");
}
public function SetImgFormat($aFormat, $aQuality=75)
{
$this->image_format = $aFormat;
$this->image_quality = $aQuality;
$aFormat = strtolower($aFormat);
$tst = true;
$supported = imagetypes();
if ($aFormat=="auto") {
if ($supported & IMG_PNG) {
$this->img_format="png";
} elseif ($supported & IMG_JPG) {
$this->img_format="jpeg";
} elseif ($supported & IMG_GIF) {
$this->img_format="gif";
} else {
JpGraphError::RaiseL(12008);
}
//(" Your PHP (and GD-lib) installation does not appear to support any known graphic formats.".
return true;
} else {
if ($aFormat=="jpeg" || $aFormat=="png" || $aFormat=="gif") {
if ($aFormat=="jpeg" && !($supported & IMG_JPG)) {
$tst=false;
} elseif ($aFormat=="png" && !($supported & IMG_PNG)) {
$tst=false;
} elseif ($aFormat=="gif" && !($supported & IMG_GIF)) {
$tst=false;
} else {
$this->img_format=$aFormat;
return true;
}
} else {
$tst=false;
}
if (!$tst) {
JpGraphError::RaiseL(12009, $aFormat);
}
//(" Your PHP installation does not support the chosen graphic format: $aFormat");
}
}
// Stream image to browser or to file
public function Stream($aFile="")
// Set the shadow around the whole image
public function SetShadow($aShowShadow=true, $aShadowWidth=4, $aShadowColor='gray@0.3')
{
$func="image".$this->img_format;
if ($this->img_format=="jpeg" && $this->image_quality != null) {
$res = @$func($this->img, $aFile, $this->image_quality);
} else {
if ($aFile != "") {
$res = @$func($this->img, $aFile);
} else {
$res = @$func($this->img);
$this->doshadow = $aShowShadow;
$this->shadow_color = $aShadowColor;
$this->shadow_width = $aShadowWidth;
$this->footer->iBottomMargin += $aShadowWidth;
$this->footer->iRightMargin += $aShadowWidth;
}
public function StrokeTitle($image, $w, $h)
{
// Stroke title
if ($this->title->t !== '') {
$margin = 3;
$y = $this->title->margin;
if ($this->title->halign == 'center') {
$this->title->Center(0, $w, $y);
} elseif ($this->title->halign == 'left') {
$this->title->SetPos($this->title->margin+2, $y);
} elseif ($this->title->halign == 'right') {
$indent = 0;
if ($this->doshadow) {
$indent = $this->shadow_width+2;
}
$this->title->SetPos($w-$this->title->margin-$indent, $y, 'right');
}
$this->title->Stroke($image);
// ... and subtitle
$y += $this->title->GetTextHeight($image) + $margin + $this->subtitle->margin;
if ($this->subtitle->halign == 'center') {
$this->subtitle->Center(0, $w, $y);
} elseif ($this->subtitle->halign == 'left') {
$this->subtitle->SetPos($this->subtitle->margin+2, $y);
} elseif ($this->subtitle->halign == 'right') {
$indent = 0;
if ($this->doshadow) {
$indent = $this->shadow_width+2;
}
$this->subtitle->SetPos($this->img->width-$this->subtitle->margin-$indent, $y, 'right');
}
$this->subtitle->Stroke($image);
// ... and subsubtitle
$y += $this->subtitle->GetTextHeight($image) + $margin + $this->subsubtitle->margin;
if ($this->subsubtitle->halign == 'center') {
$this->subsubtitle->Center(0, $w, $y);
} elseif ($this->subsubtitle->halign == 'left') {
$this->subsubtitle->SetPos($this->subsubtitle->margin+2, $y);
} elseif ($this->subsubtitle->halign == 'right') {
$indent = 0;
if ($this->doshadow) {
$indent = $this->shadow_width+2;
}
$this->subsubtitle->SetPos($w-$this->subsubtitle->margin-$indent, $y, 'right');
}
$this->subsubtitle->Stroke($image);
}
if (!$res) {
JpGraphError::RaiseL(12010, $aFile);
}
//("Can't create or stream image to file $aFile Check that PHP has enough permission to write a file to the current directory.");
}
public function Stroke($aFileName='')
@ -392,30 +307,44 @@ HTTP headers have already been sent.</font></td></tr><tr><td><b>Explanation:</b>
$h += $this->tm+$this->bm;
// User specified width,height overrides
if ($this->iWidth !== null) {
if ($this->iWidth !== null && $this->iWidth !== 0) {
$w = $this->iWidth;
}
if ($this->iHeight!== null) {
if ($this->iHeight!== null && $this->iHeight !== 0) {
$h = $this->iHeight;
}
$this->_imageCreate($w, $h);
$this->iRGB = new RGB($this->img);
if ($this->doshadow) {
$w += $this->shadow_width;
$h += $this->shadow_width;
}
$this->_setcolor($this->iFillColor);
$this->_filledRectangle(0, 0, $w-1, $h-1);
$image = new Image($w, $h);
$image->SetImgFormat($this->image_format, $this->image_quality);
if ($this->doshadow) {
$image->SetColor($this->iFrameColor);
$image->ShadowRectangle(0, 0, $w-1, $h-1, $this->iFillColor, $this->shadow_width, $this->shadow_color);
$w -= $this->shadow_width;
$h -= $this->shadow_width;
} else {
$image->SetColor($this->iFillColor);
$image->FilledRectangle(0, 0, $w-1, $h-1);
}
$image->SetExpired($this->expired);
$this->img = $image->img;
$this->_strokeBackgroundImage();
if ($this->iDoFrame) {
$this->_setColor($this->iFrameColor);
$this->iLineWeight=$this->iFrameWeight;
$this->_rectangle(0, 0, $w-1, $h-1);
if ($this->iDoFrame && ! $this->doshadow) {
$image->SetColor($this->iFrameColor);
$image->SetLineWeight($this->iFrameWeight);
$image->Rectangle(0, 0, $w-1, $h-1);
}
// Copy all sub graphs to the container
for ($i=0; $i < $this->iCnt; ++$i) {
$this->_imageCp(
$image->CopyMerge(
$this->iGraphs[$i][0],
$this->iGraphs[$i][1]+$this->lm,
$this->iGraphs[$i][2]+$this->tm,
@ -423,16 +352,24 @@ HTTP headers have already been sent.</font></td></tr><tr><td><b>Explanation:</b>
$this->iGraphs[$i][4],
$this->iGraphs[$i][5],
$this->iGraphs[$i][6],
$this->iGraphs[$i][7]
-1,
-1, /* Full from width and height */
$this->iGraphs[$i][7]
);
}
$this->StrokeTitle($image, $w, $h);
$this->footer->Stroke($image);
// Output image
if ($aFileName == _IMG_HANDLER) {
return $this->img;
return $image->img;
} else {
$this->Headers();
$this->Stream($aFileName);
//Finally stream the generated picture
$this->cache = new ImgStreamCache();
$this->cache->PutAndStream($image, $this->cache_name, $this->inline, $aFileName);
}
}
}
// EOF