impagepng - image error bij laden
function create_graph($value, $date){
if(count($value)!=count($date)){
// The number of values in $values is not the same as in $date, ERROR.
return 'Error';
exit;
}
$array_count = count($value);
// Delete all 'NULL' records from the arrays. *****************************
for($i=0; $i<$array_count; $i++){
if($value[$i]==NULL){
unset($value[$i]);
}
if($date[$i]==NULL){
unset($date[$i]);
}
}
$array_count = count($value);
$image_width = 300;
$image_height = 360;
$graph_size = 250;
$free_space_width = $image_width - $graph_size;
$free_space_height = $image_height - $graph_size;
$image = imagecreate($image_width, $image_height);
$color_white = imagecolorallocate($image, 255, 255, 255);
$color_grey = imagecolorallocate($image, 192, 192, 192);
$color_blue = imagecolorallocate($image, 0, 148, 218);
$color_black = imagecolorallocate($image, 0, 0, 0);
// Create the rectangle around the graph (outer lines). *******************
imagerectangle($image, $free_space_width, 1, $image_width-1, $image_height-$free_space_height, $color_black);
// Create the grid lines. *************************************************
for ($i=1; $i<$array_count-1; $i++){
$grid_break = $graph_size / ($array_count-1); // Create a grid line every graph_width / (the number of values in the array-1) pixels.
imageline($image, $i*$grid_break+$free_space_width, 2, $i*$grid_break+$free_space_width, $image_height-$free_space_height-1, $color_grey); // Y-axis
imageline($image, $free_space_width+1, $i*$grid_break, $image_width-2, $i*$grid_break, $color_grey); // X-axis
}
// Set the Y-axis legend. *************************************************
if(min($value)==NULL){
$lowest_value=0; // If there is a NULL value in the value array, make the lowest number 0 instead of NULL.
}
imagestringup($image, 4, 1, $graph_size/2, "Weight", $color_black); // Y-axis name
for($i=0; $i<$array_count; $i++){
// The Y-axis has $array_count values, thus the average, that should be displayed at each grid line, should increase with max($values)/$array_count every time, starting at min($values).
$y_value = number_format(max($value)/$array_count, 1, ".", " ");
$y_value = ($i+1) * $y_value;
if($i==0){
// The first number on the Y-axis should always be 0.
$y_value = 0;
}
if($i==$array_count-1){
// The last number on the Y-axis should always be max($value)
$y_value = max($value);
}
imagestring($image, 3, $free_space_width-20, $graph_size-($grid_break*$i), $y_value, $color_black);
}
// Set the X-axis legend. *************************************************
imagestring($image, 4, $image_width/2, $image_height-20, "Date", $color_black); // X-axis name
for($i=0; $i<$array_count; $i++){
$text_to_left = 5; // Number of pixels the text should be shifted to the left from its origin.
if($i==($array_count-1)){
$text_to_left = 15; // The last text string that is going to be printed should be shifted slightly more to the left, so that it doesn't get outside the image's borders.
}
imagestringup($image, 3, $i*$grid_break+$free_space_width-$text_to_left, $image_height-32, $date[$i], $color_black); // Set the highest number in the value array to be the highest number on the y-axis.
}
// Create the graph. ******************************************************
for($i=0; $i<$array_count; $i++){
// For each value in the array, determine the y-value in the graph (relatively).
$y_height[$i] = $value[$i]/max($value);
$y_height[$i] = $y_height[$i]*$graph_size;
}
for ($i=0; $i<$array_count; $i++){
imageline($image, $free_space_width+($i*$grid_break), $graph_size-$y_height[$i], $free_space_width+(($i+1)*$grid_break), $graph_size-$y_height[$i+1], $color_blue);
imagerectangle($image, $free_space_width+($i*$grid_break)-1, $graph_size-$y_height[$i]-1, $free_space_width+($i*$grid_break)+1, $graph_size-$y_height[$i]+1, $color_blue);
}
header("Content-type: image/png");
imagejpeg($image);
imagedestroy($image);
}
Als ik op een LEGE pagina alleen neerzet
create_graph(waardes)
(voor de liefhebbers die willen testen, hier de code):
$value=array(1, 2, 4, 4, 9, 6, 7, 8);
$date=array("10-10-2010", "10-10-2010", "10-10-2010", "10-10-2010", "10-10-2010", "10-10-2010", "10-10-2010", "10-10-2010");
create_graph($value,$date);
komt er een heel mooi grafiekje uitrollen.
Zodra ik hem echter als plaatje wil gebruiken op een pagina van mn site, krijg ik een volledig witte pagina waarop staat: "De afbeelding ... kan niet worden weergegeven, omdat deze fouten bevat". De betreffende pagina start met ob_start() en eindigt met ob_end_flush(). Als ik deze twee regels weg haal, krijg ik wel de normale pagina te zien, met een error:
Warning: Cannot modify header information - headers already sent by (output started at C:\...\php_includes\header.inc.php:43) in C:\xampp\htdocs\iProgrS\function_graph.php on line 104
�����JFIF���������>CREATOR: gd-jpeg v1.0 (using IJG JPEG v70), default quality ���C� $.' ",#(7),01444'9=82<.342���C 2!!22222222222222222222222222222222222222222222222222���
Ik denk dat het aan een header probleem oid ligt, maar ik zou niet weten hoe ik het op moet lossen. Weet iemand wellicht wat te doen?
Gewijzigd op 14/10/2010 23:26:44 door Stephan Geen
Lees je error eens opnieuw. Je bent idd al dicht bij als je weet dat het aan headers ligt. Je error zegt namelijk dat er al output verstuurd is geweest in headers.inc.php op lijn 43 (of in de buurt ervan)
en gebruik eens php start en stop tekens of code tags..