What is causing the unusual output when trying to create charts in Drupal 7 using the pChart library?
The output you are seeing is simply the PNG file that is being outputs it would be a text file. That is because Drupal already sets the "Content-type" header to "text/html; charset=utf-8," while the method you are calling is setting it to "image/png."

   function stroke($BrowserExpire=FALSE)
    {
     if ( $this-&gt;TransparentBackground ) {imagealphablending($this-&gt;Picture,false); imagesavealpha($this-&gt;Picture,true); }

     if ( $BrowserExpire )
      {
       header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
       header("Cache-Control: no-cache");
       header("Pragma: no-cache");
      }

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

   /* Automatic output method based on the calling interface */
   function autoOutput($FileName="output.png")
    {
     if (php_sapi_name() == "cli")
      $this-&gt;Render($FileName);
     else
      $this-&gt;Stroke();
    }


What you could do is:


Use pChart to create a file containing the chart you want to show
Output a &lt;img&gt; tag that references the created file




If imagepng() used the PHP stream wrappers (which is true for file_get_contents()), the following code would work.

/* Render the picture (choose the best way) */
$uri = file_create_filename('example.basic.png', file_build_uri('pictures'));
$myPicture-&gt;Render($uri);
echo('&lt;img src="' . file_create_url($uri) . '" /&gt;');


In this case, the code is slightly more complex.

/* Render the picture (choose the best way) */
$uri = file_create_filename('example.basic.png', file_build_uri('pictures'));
$instance = file_stream_wrapper_get_instance_by_uri($uri);
if ($filename = $instance-&gt;realpath()) {
  $myPicture-&gt;Render($filename);
  echo('&lt;img src="' . file_create_url($uri) . '" /&gt;');
}


I wrote a conditional statement around $filename = $instance-&gt;realpath() because DrupalLocalStreamWrapper::getLocalPath(), the method called by DrupalLocalStreamWrapper::realpath(), can return FALSE when it cannot determinate the real path.  

file_create_filename() is a function that returns a unique filename; in the case there is already a file with the name passed as argument, the function adds a number at the end of the filename, checking every time the file doesn't exist already. If "example.basic.png" is the name of an existing file, the function will try with "example.basic_0.png"; if there is already a file using that filename, it will try with "example.basic_1.png"; the loop is continued until the function find a filename that is not already used.

In the case you need to the filename to a function, as in this case, try first the file_create_filename($filename, file_build_uri($directory)) approach, when you are not sure the function to which you are passing the filename is using PHP stream wrappers; only in the case it doesn't, you should use file_stream_wrapper_get_instance_by_uri() as in the second snippet.