3

How can I get a pchart in a specific location on my HTML page? right now when I follow the example and use

$myPicture->autoOutput();

It just overwrites my entire page and just shows the graph.

Looking at the pChart source I found inside pImage.class.php a relevant function

   function stroke()
    {
     if ( $this->TransparentBackground ) { imagealphablending($this->Picture,false); imagesavealpha($this->Picture,true); }

     header('Content-type: image/png');
     imagepng($this->Picture);
    }

I tried to change it to this but it seems to return null and I'm getting a bunch of errors about

Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename in**

   function stroke()
    {   
    ob_start(NULL,4096);
    $this->Picture;
    header('Content-type: text/html');
    return base64_encode(ob_get_clean();  
    }
Prix
  • 19,417
  • 15
  • 73
  • 132
user391986
  • 29,536
  • 39
  • 126
  • 205

2 Answers2

4

the easiest way is to split your project into 2 seperate files. One file holds your webpage and the other creates the image (chart). Let's name the second files chart.php As you mentioned, it outputs only an image. What you need to do, is to embed this file into the first one, which holds the webpage.

Add <img src="chart.php" width="x" height="y" /> within your html code.

HtH, Thor

Thor
  • 56
  • 1
  • Thanks I could do this the problem is the pages I generate are also compiling to emails and the emails must not have any external link the images must be embedded inline into the email. Right now what I do is if it displays to browser i use base64 of image and if it sends to email i embed object and use cid. Basically I need a way to get ahold of the image as an object. – user391986 Apr 05 '11 at 20:01
  • Hi again, if you already use a base64 encoded version for browsers it would help you in emails as well. Just attach the base64 encoded version of your images to the email. Most attachments are added in base64 encoded form. Add a header for the filename of the attached image to the mail as well and will be able to refer it in your mailcontent. Hope I got the problem ;) – Thor Apr 05 '11 at 20:23
  • Hey sorry I meant I have other content not generated with pChart where I use base 64 on so i'm trying to get the chart as an object so I can perform the same base 64 operations. please see 4:32 edit – user391986 Apr 05 '11 at 20:31
  • Seems to be good. You have two options, I think. You could save the PNG picture, to a file, base64 encode this file and delete the image. Or you could use this function, which outputs the image in the php-file itself. Therefor, you could access the script from your mail generating script via `$file = fopen ("http://www.example.com/chart.php", "r");` read the data to a string and encode this string with base64. – Thor Apr 05 '11 at 20:46
2

I got it working, I extended the pImage class so I added the following function:

function strokeToBase64()
{
   ob_start();
   imagepng($this->Picture);
   $contents =  ob_get_contents();
   ob_end_clean();
   return base64_encode($contents);
}

Then the PHP code that uses the pChart lib invokes the function like this:

echo $myPicture->strokeToBase64();

In this way I can easily invoke Ajax calls using jQuery in the client side without having to create image files in the server as in the above posts is mentioned.

ealbert
  • 91
  • 1
  • 3
  • Be warned, some legacy browsers (certainly IE6 and possibly some later versions) don't support this. – John Jan 03 '14 at 14:50