Hi,
The following code creates an image using GD. I can output it straight to the browser fine, but then of course I can't send any other content. I'm trying to save it to a file so that I can call it as an image later, but I keep getting the following error message.
Warning: imagepng: unable to open '/Images/chart.png' for writing in d:\pds_static\pds_v2\cm_pds_src\htdocs\hwmetricsgraph.php on line 131
header("Content-type: image/png");
$path = "/Images/chart.png";
$data = array(
"New" => 0,
"Open" => 17,
"Fixed" => 5,
"Closed" => 48);
$width = 500;
$height = 250;
//create canvas
$image = imagecreate($width, $height);
//$image = imagecreatefrompng ($path);
// colors
// NB these value are in decimals, they may be given in hex instead
// whichever color is at the top here is used as the background color
// int imagecolorallocate (resource image, int red, int green, int blue)
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$orange = imagecolorallocate($image, 255, 204, 51);
$yellow = imagecolorallocate($image, 255, 255, 51);
$red = imagecolorallocate($image, 204, 0, 0);
$green = imagecolorallocate($image, 102, 204, 102);
$purple = imagecolorallocate($image, 102, 102, 153);
$blue = imagecolorallocate($image, 0, 0, 255);
$gray = imagecolorallocate($image, 204, 204, 204);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
//$all_values = array_merge($new, $open, $fixed, $closed);
$maxval = max($data); //the largest value in $data
$num_values = 4; //the number of elements in $data
$vmargin = 25; // top and bottom vertical margin where we will place title and x-axis labels
$hmargin = 40; // left horizontal margin for y-axislabels
$plot_height = $height - (2 * $vmargin); // total height of plot
//$plot_width = $num_values * $bar_width; // total width of plot of plot
$plot_width = $width - $hmargin;
// plot frame
//int imagerectangle (resource image, int x1, int y1, int x2, int y2, int col)
imagerectangle($image, $hmargin, $vmargin, $hmargin + $plot_width-1, $vmargin + $plot_height, $black);
// title
$titlefont = 5;
$title = "Number of Hardware problems for COOK";
// pixel-width of title
//the width of each individual letter for the font * the number of characters
$title_pix_width = imagefontwidth($titlefont) * strlen($title);
$x_title_corner = ($hmargin + ($plot_width - $title_pix_width)/2); // center the title
$x_title_corner = max(1, $x_title_corner); // force positive coordinates, 1 > 0.5 so always a positive
$y_title_corner = 1; // distance from top
//int imagestring (resource image, int font, int x, int y, string, int col)
imagestring($image, $titlefont, $x_title_corner, $y_title_corner, $title , $black); //create the title label
// y labels and grid lines
$labelfont = 2;
//$no_gridlines = round_up_ten($maxval)/10;
$no_gridlines = $maxval/10;
//$no_gridlines = 9; // number of grid lines
$data_unit_pixel_length = $maxval / $no_gridlines; // data units between grid lines
//$ylabel_data = $maxval / $no_gridlines; // data units between grid lines
$grid_pix_gap = $plot_width / ($no_gridlines + 1); // pixels between grid lines
for ($i = 0; $i <= ($no_gridlines + 1); $i++)
{
// iterate over y ticks
//$yaxis_scale = (int)($i * $ylabel_data); // height of grid line in units of data
$xaxis_label = ($i * 10); // set gaps between gridlines, grid gives 10px increments
//this is also used as the value of the labels on the y-axis
$gridline_x_pos = $hmargin + $grid_pix_gap + ($i*$grid_pix_gap); // start position of grid line in pixels
$gridline_end_y = $vmargin + $plot_height + ($vmargin/2);
$label_pix_width = imagefontwidth($labelfont) * strlen($xaxis_label); // pixel-width of label
$label_pix_height = imagefontheight($labelfont); // pixel-height of label
//$x_label_start = ($hmargin - $label_pix_width)/2))+$grid_pix_gap*$i; //center the label in the horizontal margin
$x_label_start = $hmargin + ($grid_pix_gap*$i) - ($label_pix_width/2);
$x_label_start = max(1, $x_label_start); // force positive coordinates
ImageLine($image, $gridline_x_pos, $vmargin, $gridline_x_pos, $gridline_end_y, $black);
//create the label
//imagestring($image, $labelfont, $x_label_start, $gridline_x_pos - (int)($label_pix_height/2), $xaxis_label, $black);
ImageString($image, $labelfont, $x_label_start, $gridline_end_y, $xaxis_label, $black);
}
// columns and x labels
$padding = 3; // half of spacing between columns
$axis_scale = $plot_width / (($no_gridlines+1)*$data_unit_pixel_length); // pixels per data unit
$bar_width = floor($plot_height / $num_values); // distance between bars
for ($i = 0; list($label_name, $label_value) = each($data); $i++) //bars and labels
{
// horizontal columns
$x_start = $hmargin;
$x_end = $hmargin + ($label_value*$axis_scale);
$y_end = $vmargin + (($i+1)*$bar_width) - $padding;
$y_start = $vmargin + ($i*$bar_width) + $padding;
ImageFilledRectangle($image, $x_start, $y_start, $x_end, $y_end, $green);
ImageRectangle($image, $x_start, $y_start, $x_end, $y_end, $black);
// x labels
$label_width = ImageFontWidth($labelfont) * strlen($label_name);
$label_height = ImageFontHeight($labelfont);
$label_xpos = $hmargin-$label_width;
$label_ypos = $y_end - ($bar_width/2) - ($label_height/2) ; // distance from x axis
imagestring($image, $labelfont, $label_xpos, $label_ypos, $label_name, $black);
//value labels
$value_label_width = ImageFontWidth($labelfont) * strlen($label_value);
$value_label_height = ImageFontHeight($labelfont);
$value_label_xpos = $hmargin+($x_end/2)-($label_width/2);
$value_label_ypos = $y_end - ($bar_width/2) - ($label_height/2) ; // distance from x axis
if ($label_value != 0)
{
imagestring($image, $labelfont, $value_label_xpos, $value_label_ypos, $label_value, $black);
}
}
imagepng($image);
//imagepng($image, $path);
imagedestroy($image);