drawFrom* modification - Offset and maxWidth
I needed to draw all the background of my charting area with different (dynamic) png images. As the background of the rest of my chart had to be transparent I also had to "cut" the image at the right border of my charting area.
To do this I included a $Format parameter into the drawFrom* and drawFromPicture methods.
The following options are supported.
Width - reducing width
Height - reducing height
OffsetX - shifting the image to the left and reducing the width
OffsetY - shifting the image down and reducing the height
btw. great work jean-damien, I really love pChart.
Code:
/* Load a PNG file and draw it over the chart */
function drawFromPNG($X,$Y,$FileName,$Format="")
{ $this->drawFromPicture(1,$FileName,$X,$Y,$Format); }
/* Load a GIF file and draw it over the chart */
function drawFromGIF($X,$Y,$FileName,$Format="")
{ $this->drawFromPicture(2,$FileName,$X,$Y,$Format); }
/* Load a JPEG file and draw it over the chart */
function drawFromJPG($X,$Y,$FileName,$Format="")
{ $this->drawFromPicture(3,$FileName,$X,$Y,$Format); }
function getPicInfo($FileName)
{
$Infos = getimagesize($FileName);
$Width = $Infos[0];
$Height = $Infos[1];
$Type = $Infos["mime"];
if ( $Type == "image/png") { $Type = 1; }
if ( $Type == "image/gif") { $Type = 2; }
if ( $Type == "image/jpeg ") { $Type = 3; }
return(array($Width,$Height,$Type));
}
/* Generic loader function for external pictures */
function drawFromPicture($PicType,$FileName,$X,$Y,$Format="")
{
if ( file_exists($FileName) )
{
list($Width,$Height) = $this->getPicInfo($FileName);
if ( $PicType == 1 )
{ $Raster = imagecreatefrompng($FileName); }
elseif ( $PicType == 2 )
{ $Raster = imagecreatefromgif($FileName); }
elseif ( $PicType == 3 )
{ $Raster = imagecreatefromjpeg($FileName); }
else
{ return(0); }
if(isset($Format["Width"]) && $Format["Width"]<$Width) $Width = $Format["Width"];
if(isset($Format["Height"]) && $Format["Height"]<$Height) $Height = $Format["Height"];
if(isset($Format["OffsetX"]) && $Format["OffsetX"]<$Width) { $Xo = $Format["OffsetX"]; $Width-= $Xo;} else {$Xo = 0;}
if(isset($Format["OffsetY"]) && $Format["OffsetY"]<$Height) { $Yo = $Format["OffsetY"]; $Height-= $Yo;} else {$Yo = 0;}
$RestoreShadow = $this->Shadow;
if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 )
{
$this->Shadow = FALSE;
if ( $PicType == 3 )
$this->drawFilledRectangle($X+$this->ShadowX,$Y+$this->ShadowY,$X+$Width+$this->ShadowX,$Y+$Height+$this->ShadowY,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa));
else
{
$TranparentID = imagecolortransparent($Raster);
for ($Xc=0;$Xc<=$Width-1;$Xc++)
{
for ($Yc=0;$Yc<=$Height-1;$Yc++)
{
$RGBa = imagecolorat($Raster,$Xc,$Yc);
$Values = imagecolorsforindex($Raster,$RGBa);
if ( $Values["alpha"] < 120 )
{
$AlphaFactor = floor(($this->Shadowa / 100) * ((100 / 127) * (127-$Values["alpha"])));
$this->drawAlphaPixel($X+$Xc+$this->ShadowX,$Y+$Yc+$this->ShadowY,$AlphaFactor,$this->ShadowR,$this->ShadowG,$this->ShadowB);
}
}
}
}
}
$this->Shadow = $RestoreShadow;
imagecopy($this->Picture,$Raster,$X,$Y,$Xo,$Yo,$Width,$Height);
imagedestroy($Raster);
}
}
added:Code:
if(isset($Format["Width"]) && $Format["Width"]<$Width) $Width = $Format["Width"];
if(isset($Format["Height"]) && $Format["Height"]<$Height) $Height = $Format["Height"];
if(isset($Format["OffsetX"]) && $Format["OffsetX"]<$Width) { $Xo = $Format["OffsetX"]; $Width-= $Xo;} else {$Xo = 0;}
if(isset($Format["OffsetY"]) && $Format["OffsetY"]<$Height) { $Yo = $Format["OffsetY"]; $Height-= $Yo;} else {$Yo = 0;}
edited:Code:
/* Load a PNG file and draw it over the chart */
function drawFromPNG($X,$Y,$FileName,$Format="")
{ $this->drawFromPicture(1,$FileName,$X,$Y,$Format); }
/* Load a GIF file and draw it over the chart */
function drawFromGIF($X,$Y,$FileName,$Format="")
{ $this->drawFromPicture(2,$FileName,$X,$Y,$Format); }
/* Load a JPEG file and draw it over the chart */
function drawFromJPG($X,$Y,$FileName,$Format="")
{ $this->drawFromPicture(3,$FileName,$X,$Y,$Format); }
[...]
imagecopy($this->Picture,$Raster,$X,$Y,$Xo,$Yo,$Width,$Height);
example:Code:
require("pChart/class/pDraw.class.php");
require("pChart/class/pImage.class.php");
$i = new pImage(734,200,null,TRUE);
$i->setShadow(TRUE);
$i->drawFromPNG(0, 0, "images/sunCloud.png",array("OffsetX"=>100));
$i->drawFromJPG(210, 0, "images/sunCloud.jpg",array("Width"=>200));
$i->drawFromJPG(420, 0, "images/sunCloud.jpg");
$i->setShadow(FALSE);
$i->stroke();
output: