View unanswered posts | View active topics It is currently Thu Mar 28, 2024 8:48 am



Reply to topic  [ 1 post ] 
drawFrom* modification - Offset and maxWidth 
Author Message
New pChart user
New pChart user

Joined: Mon Sep 12, 2011 7:17 pm
Posts: 1
Post 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:


Attachments:
File comment: From left to right:
OffsetX 100, Width 200, normal

shadowTest.png
shadowTest.png [ 56.66 KiB | Viewed 7094 times ]
Mon Sep 12, 2011 7:53 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron