I’ve created my own custom Spider Chart generator using PHP and the GD Library. Basically, the user answers a series of questions, and a Spider Chart is generated based on the answers to the questions.
Overall, everything works great. The chart that’s generated is mathematically accurate (though, the quiz needs to be reworded entirely), and it seems to work quickly enough. However, what I’ve noticed is that when I go and try to save the image from Firefox (version 15.0.1 on Snowleopard), the image itself cannot be opened. Saving the image from any other browser yields a working image that can be opened in any image editor.
What I find particularly interesting is WHEN it breaks. I’ll see if I can explain this accurately.
The full working version is [no longer here], and here’s all the relevant bits of code involved in the processing and generation of the Chart itself:
chart.php (process POST data)
$r = 300; //Radius
//Processing of information passed from the previous form yields an array $cargs
//$cargs contains key value pairs. Values are of type float with a range 0-$r
$chart = new Chart($r); //Generates Canvas and Circle
$chart->generate($cargs); //Generates Polygon based on $cargs
header("Content-type: image/png");
imagesavealpha($chart->image, true);
imagepng($chart->image);
imagedestroy($chart->image);
?>
SS_Chart.php (Chart Object)
class Chart
{
public $radius;
public $diameter;
public $image;
private $_labels;
private $_values;
private $_n;
public function Chart($r=300)
{
$this->radius = $r;
$this->diameter = 2*$r;
$this->image = $this->_get_transparent_canvas($this->diameter, $this->diameter);
$this->_draw_circle();
}
public function generate($arr)
{
if(count($arr)>2)
{
$red = imagecolorallocate($this->image, 255, 0, 0);
$this->_n = count($arr);
$this->_labels = array_keys($arr);
$this->_values = array_values($arr);
$coors = array();
for($i=0;$i<$this->_n;$i++)
{
//Generate coordinates....
}
imagefilledpolygon($this->image, $coors, $this->_n, $red);
}
else
trigger_error('Number of $key=>$value pairs in arguments must be 3 or greater', E_USER_ERROR);
}
private function _get_transparent_canvas($w, $h)
{
$canvas = imagecreatetruecolor($w, $h);
imagealphablending($canvas, true);
$transparent = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
imagefill($canvas, 0, 0, $transparent);
return $canvas;
}
private function _draw_circle()
{
$white = imagecolorallocate($this->image, 255, 255, 255);
imagearc($this->image, $this->radius, $this->radius, $this->diameter, $this->diameter, 0, 360, $white);
}
}
Here’s where it gets interesting: When commenting out $chart->generate($cargs); in chart.php, Firefox will yield a working and editable Transparent png according to the dimensions, as well as a white circle.
However, if I comment out everything within public function generate in SS_Chart.php and leave $chart->generate($cargs); uncommented (basically resulting in a method that runs, but doesn’t actually do anything), the resulting png can no longer be opened.
How is that possible??
I haven’t found much on this issue apart from the odd forum here and there describing some difficulties in getting Firefox to jive with the GD Library, but this is the first time I’ve ever encountered anything like this.
- If it were a problem with my code, wouldn’t the issue be prevalent across all browsers?
- Does this have more to do with Scoping, and the fact that the main GD functions are utilized within an Object?
- Moreover, does this issue happen for anybody else?
- Has anybody run into a problem like this before?
- Is this even a known problem?
This is actually more of a curiosity than it is a real “problem”, but any insight into the matter would be greatly appreciated.
Look into the source of that SAVED image
When you save it in Firefox, fireox doesn’t save what you see. What it does is – it makes an another GET request on save.
Obviously all POST data is lost hence the error. I’m not sure about your commenting out thing but I guess that’s what it is.