Home » Images » Image_GraphViz » Bug #8380
Use readfile instead of fread and echo
Details
| Request #8380 | Use readfile instead of fread and echo |
|---|---|
| Submitted | 2006-08-07 22:25 UTC |
| From | php at adaniels dot nl |
| Assigned | jausions |
| Status | Closed |
| Package | Image_GraphViz |
| PHP Version | 5.1.4 |
| Roadmaps | (Not assigned) |
Comments
[2006-08-07 22:25 UTC] php at adaniels dot nl
Description:
------------
First of all, love graphviz and your package. I have a small suggestion though.
Currently you are using fread and than echo. When the generated image is large, this will affect memory usage. It might be better to use fread instead.
Adding a step where the image is saved to file, also gives an option to save without reading it back to php and than writing again.
Saying this, I had not yet rendered any image where the size was a real problem.
Test script:
---------------
// output image
$gv->image();
// fetch image
$data = $gv->fetch();
// save image to file
$outputfile = $gv->saveImage();
rename($outputfile, dirname(__FILE__) . '/img/');
Expected result:
----------------
Currently:
function image($format = 'svg')
{
if ($data = $this->fetch($format)) {
...
echo $data;
}
}
function fetch($format = 'svg')
{
if ($file = $this->saveParsedGraph()) {
$outputfile = $file . '.' . $format;
$command = $this->graph['directed'] ? $this->dotCommand : $this->neatoCommand;
$command .= ' -T' . escapeshellarg($format) . ' -o' . escapeshellarg($outputfile) . ' ' . escapeshellarg($file);
@`$command`;
@unlink($file);
$fp = fopen($outputfile, 'rb');
if ($fp) {
$data = fread($fp, filesize($outputfile));
fclose($fp);
@unlink($outputfile);
}
return $data;
}
return FALSE;
}
Actual result:
--------------
Better might be:
function image($format = 'svg')
{
if ($outputfile = $this->saveImage($format) && filesize($outputfile)) {
...
fileread($outputfile);
@unlink($outputfile);
}
}
function fetch($format = 'svg')
{
if ($outputfile = $this->saveImage($format)) {
$fp = fopen($outputfile, 'rb');
if ($fp) {
$data = fread($fp, filesize($outputfile));
fclose($fp);
@unlink($outputfile);
}
return $data;
}
return FALSE;
}
function saveImage($format = 'svg')
{
if ($file = $this->saveParsedGraph()) {
$outputfile = $file . '.' . $format;
$command = $this->graph['directed'] ? $this->dotCommand : $this->neatoCommand;
$command .= ' -T' . escapeshellarg($format) . ' -o' . escapeshellarg($outputfile) . ' ' . escapeshellarg($file);
@`$command`;
@unlink($file);
return $outputfile;
}
return FALSE;
}