{"id":2197,"date":"2012-02-29T14:09:01","date_gmt":"2012-02-29T14:09:01","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?page_id=2197"},"modified":"2012-03-01T11:15:59","modified_gmt":"2012-03-01T11:15:59","slug":"drawing-dynamic-arcs-with-php","status":"publish","type":"page","link":"https:\/\/www.simonbattersby.com\/blog\/drawing-dynamic-arcs-with-php\/","title":{"rendered":"Drawing dynamic arcs with PHP"},"content":{"rendered":"<p>A few years ago I was managing a web project where part of the requirement was to render a coloured rainbow representing achievement against a number of dimensions. The (existing) solution used Flash to achieve this, but this caused our users a lot of issues because they (variously) had flash disabled, the wrong version etc etc<\/p>\r\n<p>Our developers at the time had dismissed alternate versions as not possible. However, I had a little spare time one weekend and built a solution using PHP.<\/p>\r\n<h3>The code<\/h3>\r\n<p>This page is just a simple html form allowing the values to be entered as percentages. In the production system these would be calculated from a range of metrics, which I&#8217;m emulating here using the form. Then, these values are used to dynamically create the image using this PHP. The approach I&#8217;ve taken is to draw a series of concentric segments, decreasing in size, so the subsequent segments are drawn on top of the previous segments, resulting in the concentric arcs.<\/p>\r\n<pre>&lt;?php\r\n<span class=\"code_comment\">\/\/ create a 600 by 300px image\r\n<\/span>$Image = imagecreatetruecolor(600, 300);\r\n\r\n<span class=\"code_comment\">\/\/set some colours\r\n<\/span>$Grey = imagecolorallocate($Image,220,220,220);\r\n$White = imagecolorallocate($Image, 255, 255, 255);\r\n$Black  = imagecolorallocate($Image,   0,   0, 0);\r\n\r\n$Green[0]= imagecolorallocate($Image, 166, 206, 57);\r\n$Green[1] = imagecolorallocate($Image, 23, 206, 57);\r\n$Green[2] = imagecolorallocate($Image, 0, 174, 57);\r\n$Green[3] = imagecolorallocate($Image, 0, 141, 51);\r\n$Green[4] = imagecolorallocate($Image, 0, 126, 57);\r\n\r\n<span class=\"code_comment\">\/\/set the centre position for all the arcs\r\n<\/span>$CenterX = 300;\r\n$CenterY= 250;\r\n$arc = array(470,430,390,340,300);<span class=\"code_comment\">\/\/an array holding the starting point for each arc \r\n<\/span>\r\n<span class=\"code_comment\">\/\/calculate the ending value of each arc in degrees based on form values\r\n<\/span><span class=\"code_comment\">\/\/set 181 as the minimum value otherwise the arc doesn't display properly\r\n<\/span>$end[0]=max(($_GET['arc1']*1.8)+180,181);\r\n$end[1]=max(($_GET['arc2']*1.8)+180,181);\r\n$end[2]=max(($_GET['arc3']*1.8)+180,181);\r\n$end[3]=max(($_GET['arc4']*1.8)+180,181);\r\n$end[4]=max(($_GET['arc5']*1.8)+180,181);\r\n\r\n$Start=180;<span class=\"code_comment\">\/\/starting point for all the arcs\r\n<\/span>\r\nimagefill($Image,0,0,$White);<span class=\"code_comment\">\/\/white background\r\n<\/span>\r\nimagesetthickness($Image,1);<span class=\"code_comment\">\/\/set the thickness of lines to 1px\r\n<\/span>\r\n<span class=\"code_comment\">\/\/loop through all the arcs\r\n<\/span>for ($i=0; $i&lt;=4; $i++)\r\n\t{\r\n\t<span class=\"code_comment\">\/\/ draw the arcs\r\n<\/span>\timagefilledarc($Image, $CenterX, $CenterY, $arc[$i], $arc[$i], 180, 0, $Grey, IMG_ARC_EDGED);<span class=\"code_comment\">\/\/draw the whole arc in grey - to cover anything from the previous green arc\r\n<\/span>\timagefilledarc($Image, $CenterX, $CenterY, $arc[$i], $arc[$i], $Start, $end[$i], $Green[$i], IMG_ARC_EDGED);<span class=\"code_comment\">\/\/draw a green filled arc in green to the right size\r\n<\/span>\timagearc($Image, $CenterX, $CenterY, $arc[$i], $arc[$i], $Start, 0, $Black);<span class=\"code_comment\">\/\/draw a black line outside the green arc\r\n<\/span>\t}\r\n\r\nimagefilledarc($Image, $CenterX, $CenterY, 260, 260, $Start, 0, $White, IMG_ARC_EDGED);<span class=\"code_comment\">\/\/draw a white filled arc inside the last green arc \r\n<\/span>imagearc($Image, $CenterX, $CenterY, 260, 260, $Start, 0, $Black);<span class=\"code_comment\">\/\/draw the last black line inside the last arc\r\n<\/span>\r\n<span class=\"code_comment\">\/\/ output image in the browser\r\n<\/span>header(\"Content-type: image\/png\");\r\nimagepng($Image);\r\n\r\n<span class=\"code_comment\">\/\/ free memory\r\n<\/span>imagedestroy($Image);\r\n\r\n?&gt; <\/pre>\r\n\r\n<h3>The demo<\/h3>\r\n<p>Here&#8217;s the result:<\/p>","protected":false},"excerpt":{"rendered":"<p>A few years ago I was managing a web project where part of the requirement was to render a coloured rainbow representing achievement against a number of dimensions. The (existing) solution used Flash to achieve this, but this caused our users a lot of issues because they (variously) had flash disabled, the wrong version etc [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page_arcs.php","meta":{"footnotes":""},"class_list":["post-2197","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/2197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=2197"}],"version-history":[{"count":0,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/2197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=2197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}