简体   繁体   中英

Zend Framework: PHP script in img src tag causing broken image

I'm using the JpGraph library within a Zend Framework project of version 1.11. I'm trying to display the graph of some data using an img tag by including a php script in the src field, as so:

echo "<img src='vgraph.php?param1=val1&param2=val2...

When I do this, the only thing displayed as an image is the my browser's default broken image. Since typical URLs in Zend are treated as controller-action pairs, the path to the file I am including is also treated as a controller-action pair. To investigate why I wasn't getting my image, I viewed the source of the page in the browser. When I clicked on the link for the above img tag, I was directed to a page containing the error:

Invalid controller specified (vgraph.php)

I don't by any means intend to use the script as a controller. It is only supposed to display the image of a graph. I am unsure about how to make sure that Zend only uses the file's output as an image, and not to use its path as a URL.

Here is the file I am using for the image, vgraph.php :

<?php

/* graph the vital data */

require_once( 'path/to/Zend/project/web/library/jpgraph/jpgraph.php' ); 
require_once( 'path/to/Zend/project/web/library/jpgraph/jpgraph_line.php' );

/* get the needed parameters */
$id = $_GET['param1'];
$type = $_GET['param2'];
$from = $_GET['param3'];
$to = $_GET['param4'];

$graph = new Graph( $width, $height );

/* create the data and plot */
$obj = new CcFunc_VitalSigns();
$vitals = $obj->getVitalData( $id, $type, $from, $to );

/* configure everything */
$graph->setScale( 'intint' );
$graph->title->Set( "Graph of " . $type );
$graph->xaxis->title->Set( "X-Axis" );
$graph->yaxis->title->Set( "Numerical Value" );

$lp = new LinePlot( $vitals );
$lp->setColor( 'blue' );    
$lp->setWeight( 5 );

/* add the plot to the graph */
$graph->Add( $lp );

/* display the graph */
$graph->Stroke();

I've tried other solutions, such as streaming the graph to a png file and letting the src field be the URL of that file, but I was met with the same problem, even after adding a RewriteRule to apache to prevent redirection of .png files. 我已经尝试过其他解决方案,比如图形流传输到png文件,并让src文件的URL,但我遇到了同样的问题,甚至添加后RewriteRule给了Apache,防止重定向.png文件。 I've settled upon this method as the most comfortable (potential) solution.

I didn't say this before, but the code where the img tag is echo'd is not a controller, and is actually located in path/to/Zend/roject/web/application/scripts/views . 我以前没有说过,但是回显img标签的代码不是控制器,并且实际上位于path/to/Zend/roject/web/application/scripts/viewsThe script vgraph.php is located in path/to/Zend/project/web/library .

You need to move the vgraph.php file to your public folder for this to work, the browser isn't able to view files outside of that.

Move it to path/to/Zend/project/web/public/vgraph.php , change your img tag to <img src="/vgraph.php?param1=val1&param2=val2... and everything should work as you expect.

When linking try in the view to specify the full path as:

<?php echo $this->baseURL();?> 

Zend View Helpers

This will return localhos/[APP]/

Hope this helps

Try to put everything in a Graph controller within a generateAction method and you can link to it:

<a href="<?php echo $this->baseURL();?>/graph/generate/?param1=val1&param2=val2...">Generate</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM