duanjiebian6712 2013-05-13 20:30
浏览 62

jpGraph不能包含PHP文件

I am trying to include one PHP file in another PHP file that creates a jpGraph image

(The reason is that I am loading mySQL data for the chart, and I want to put the login credentials into a separate file)

I know that the chart is created (because a correct image file is created) but the chart does not show up in the web page.

Here is a simplified code example:

login.inc.php

  <?php
  $lhostname="localhost";
  $lusername="joeschmack";
  $lpassword="autumnleaf";
  $ldatabase="customers";
  ?> 

accbarex1.html

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title></title>
  </head>
  <body>
    <h3>This is where I want to display my graph</h3>
    <img src="accbarex1.php">
  </body>
</html>

accbarex1.php

<?php // content="text/plain; charset=utf-8"

require_once ('../../../lib/jpgraph/jpgraph.php');
require_once ('../../../lib/jpgraph/jpgraph_bar.php');
include("./login.inc.php");


$data1y=array(-8,8,9,3,5,6);
$data2y=array(18,2,1,7,5,4);

// Create the graph. These two calls are always required
$graph = new Graph(500,400); 
$graph->SetScale("textlin");

$graph->SetShadow();
$graph->img->SetMargin(40,30,20,40);

// Create the bar plots
$b1plot = new BarPlot($data1y);
$b1plot->SetFillColor("orange");
$b1plot->value->Show();
$b2plot = new BarPlot($data2y);
$b2plot->SetFillColor("blue");
$b2plot->value->Show();

// Create the grouped bar plot
$gbplot = new AccBarPlot(array($b1plot,$b2plot));

// ...and add it to the graPH
$graph->Add($gbplot);

$graph->title->Set("Accumulated bar plots");
$graph->xaxis->title->Set("X-title");
$graph->yaxis->title->Set("Y-title");

//$graph->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

// Display the graph
$graph->Stroke();

//save to file
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);

?>

The file shows the correct chart, but the web page accbarex1.html shows a broken image. If I comment out the line

include("./login.inc.php");

then both work.

Why? And how can I include a file in this situation?

Edit 5-13-2013:

Given that the include line is active. This helps (both file and embedded chart work)

  • ./login.inc.php does not exist
  • ./login.inc.php is empty

This does not help (only file works, embedded chart does not work)

  • use absolute path for include file
  • ./login.inc.php contains the line bla // PHP error
  • ./login.inc.php contains the line $i=1; // no PHP error, no PHP tags

Edit 5-14-2013:

Some slight progress: Firefox shows the same behavior, but at least I get an error message. Error console says:

Image corrupt or truncated: http:// .. acbarex1.php

  • 写回答

1条回答 默认 最新

  • dqpdb82600 2013-05-16 02:33
    关注

    I found what the problem was. I had extra characters after the closing ?>, but I could not see them because they were spaces and newline. These characters messed up the jpGraph image.

    Before:

      <?php
      $lhostname="localhost";
      $lusername="joeschmack";
      $lpassword="autumnleaf";
      $ldatabase="customers";
      ?><SPACE><SPACE><NEWLINE>
    

    After:

      <?php
      $lhostname="localhost";
      $lusername="joeschmack";
      $lpassword="autumnleaf";
      $ldatabase="customers";
      ?>
    

    That fixed it! Beware the extra characters!

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大