herghost Posted March 13, 2010 Share Posted March 13, 2010 Hi all, I am trying to create a graph from some variables, here is my code: <?php header("Content-type:image/png"); include('serverauth.php'); $query = "https://" . $ip . ":2087/json-api/loadavg"; $curl = curl_init(); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); $header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$whmhash); curl_setopt($curl,CURLOPT_HTTPHEADER,$header); curl_setopt($curl, CURLOPT_URL, $query); $result = curl_exec($curl); if ($result == false) { error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query"); } curl_close($curl); $var = $result; $one = $var[8] . $var[9] . $var[10] . $var[11]; $five = $var[22] . $var[23] . $var[24] . $var[25]; $fifteen = $var[39] . $var[40] . $var[41] . $var[42]; ?> <?php // read the post data $data = array($one,$five,$fifteen); $sum = array_sum($data); $height = 255; $width = 320; $im = imagecreate($width,$height); // width , height px $white = imagecolorallocate($im,255,255,255); $black = imagecolorallocate($im,0,0,0); $red = imagecolorallocate($im,255,0,0); imageline($im, 10, 5, 10, 230, $black); imageline($im, 10, 230, 300, 230, $black); $x = 15; $y = 230; $x_width = 20; $y_ht = 0; for ($i=0;$i<7;$i++){ $y_ht = ($data[$i]/$sum)* $height; imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red); imagestring( $im,2,$x-1,$y+10,$data[$i],$black); $x += ($x_width+20); } imagejpeg($im); ?> however I am just getting an missing image x where the graph should appear? Any ideas? The GD library is installed so that shouldn't be a problem! Thanks Link to comment https://forums.phpfreaks.com/topic/195116-gd-graph-help/ Share on other sites More sharing options...
cags Posted March 13, 2010 Share Posted March 13, 2010 You are sending a Content-Type header to say the image is a png, but you are saving it with imagejpeg, this may not be causing your problem but it certainly won't help. Trying using imagepng instead. Another thing that would cause problems is if you have display_errors and error_reporting set. If an error occurs the error message will appear in the image. Try saving the image to file, opening it with a text editor and look for an error message at the start of the file. Link to comment https://forums.phpfreaks.com/topic/195116-gd-graph-help/#findComment-1025635 Share on other sites More sharing options...
herghost Posted March 14, 2010 Author Share Posted March 14, 2010 Hi Cags, Thanks sorted that but still have a problem, it should now be saving to file but I get this message: Unable to open 'http://localhost/jolly/office/serverfuctions/graphs/chart1.png' for writing: Invalid argument in C:\wamp\www\jolly\office\serverfuctions\avload.php on line 96 There is no error in the .png file and its chmod 777? Cheers Link to comment https://forums.phpfreaks.com/topic/195116-gd-graph-help/#findComment-1025827 Share on other sites More sharing options...
herghost Posted March 14, 2010 Author Share Posted March 14, 2010 Here is the code for avload: <?php include('serverauth.php'); $query = "https://" . $ip . ":2087/json-api/loadavg"; $curl = curl_init(); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); $header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$whmhash); curl_setopt($curl,CURLOPT_HTTPHEADER,$header); curl_setopt($curl, CURLOPT_URL, $query); $result = curl_exec($curl); if ($result == false) { error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query"); } curl_close($curl); $var = $result; $one = $var[8] . $var[9] . $var[10] . $var[11]; $five = $var[22] . $var[23] . $var[24] . $var[25]; $fifteen = $var[39] . $var[40] . $var[41] . $var[42]; ?> <?php // read the post data //$values = array($one,$five,$fifteen); $values = array("jan" =>1, "feb" =>2 ); $img_width=450; $img_height=300; $margins=20; # ---- Find the size of graph by substracting the size of borders $graph_width=$img_width - $margins * 2; $graph_height=$img_height - $margins * 2; $img=imagecreate($img_width,$img_height); $bar_width=20; $total_bars=count($values); $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); # ------- Define Colors ---------------- $bar_color=imagecolorallocate($img,0,64,128); $background_color=imagecolorallocate($img,240,240,255); $border_color=imagecolorallocate($img,200,200,200); $line_color=imagecolorallocate($img,220,220,220); # ------ Create the border around the graph ------ imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color); imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color); # ------- Max value is required to adjust the scale ------- $max_value=max($values); $ratio= $graph_height/$max_value; # -------- Create scale and draw horizontal lines -------- $horizontal_lines=20; $horizontal_gap=$graph_height/$horizontal_lines; for($i=1;$i<=$horizontal_lines;$i++){ $y=$img_height - $margins - $horizontal_gap * $i ; imageline($img,$margins,$y,$img_width-$margins,$y,$line_color); $v=intval($horizontal_gap * $i /$ratio); imagestring($img,0,5,$y-5,$v,$bar_color); } # ----------- Draw the bars here ------ for($i=0;$i< $total_bars; $i++){ # ------ Extract key and value pair from the current pointer position list($key,$value)=each($values); $x1= $margins + $gap + $i * ($gap+$bar_width) ; $x2= $x1 + $bar_width; $y1=$margins +$graph_height- intval($value * $ratio) ; $y2=$img_height-$margins; imagestring($img,0,$x1+3,$y1-10,$value,$bar_color); imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color); imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); } $temp_chart_file_name = "graphs/chart1.png"; imagepng($img, $temp_chart_file_name,0); ?> Link to comment https://forums.phpfreaks.com/topic/195116-gd-graph-help/#findComment-1025937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.