{"id":3651,"date":"2017-01-11T05:40:57","date_gmt":"2017-01-11T05:40:57","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3651"},"modified":"2019-04-25T13:00:59","modified_gmt":"2019-04-25T13:00:59","slug":"codephp-jpgraph-how-to-output-your-graph-as-a-base64-encoded-image","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/codephp-jpgraph-how-to-output-your-graph-as-a-base64-encoded-image\/","title":{"rendered":"[CODE\/PHP] JpGraph: How to output your graph as a base64 encoded image"},"content":{"rendered":"<p>Some times you just want to output the image created by your <code>$graph<\/code> object without having to create a separate .php script that would need to receive a bunch of parameters.<\/p>\n<p>Here&#8217;s a function you can pass your <code>$graph<\/code> object right before the <code>$graph-&gt;Stroke();<\/code> call<\/p>\n<p>[pastacode lang=&#8221;php&#8221; manual=&#8221;function%20graphInSrc(%24graph%2C%20%24width%2C%20%24height)%20%7B%0A%20%20%24img%20%3D%20%24graph-%3EStroke(_IMG_HANDLER)%3B%0A%20%20ob_start()%3B%0A%20%20imagepng(%24img)%3B%0A%20%20%24img_data%20%3D%20ob_get_contents()%3B%0A%20%20ob_end_clean()%3B%0A%0A%20%20echo%20&#8217;%3Cimg%20width%3D%22&#8242;.%24width.&#8217;%22%20height%3D%22&#8242;.%24height.&#8217;%22%20src%3D%22data%3Aimage%2Fpng%3Bbase64%2C&#8217;.base64_encode(%24img_data).&#8217;%22%2F%3E&#8217;%3B%0A%7D&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some times you just want to output the image created by your $graph object without having to create a separate .php script that would need to receive a bunch of parameters. Here&#8217;s a function you can pass your $graph object right before the $graph-&gt;Stroke(); call [pastacode lang=&#8221;php&#8221; manual=&#8221;function%20graphInSrc(%24graph%2C%20%24width%2C%20%24height)%20%7B%0A%20%20%24img%20%3D%20%24graph-%3EStroke(_IMG_HANDLER)%3B%0A%20%20ob_start()%3B%0A%20%20imagepng(%24img)%3B%0A%20%20%24img_data%20%3D%20ob_get_contents()%3B%0A%20%20ob_end_clean()%3B%0A%0A%20%20echo%20&#8217;%3Cimg%20width%3D%22&#8242;.%24width.&#8217;%22%20height%3D%22&#8242;.%24height.&#8217;%22%20src%3D%22data%3Aimage%2Fpng%3Bbase64%2C&#8217;.base64_encode(%24img_data).&#8217;%22%2F%3E&#8217;%3B%0A%7D&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[1564,1182],"class_list":["post-3651","post","type-post","status-publish","format-standard","hentry","category-code","tag-jpgraph","tag-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-WT","jetpack-related-posts":[{"id":3656,"url":"https:\/\/www.gubatron.com\/blog\/code-arduino-4-bit-led-counter\/","url_meta":{"origin":3651,"position":0},"title":"[CODE] Arduino 4 BIT LED binary counter","author":"gubatron","date":"January 11, 2017","format":false,"excerpt":"Here's my \"Hello World\" on Arduino. A 4-bit binary counter for arduino. If you add another LED on the 5th pin, then it can count from 0 to 31 (32 values) Circuit wired by @KataySantos [pastacode lang=\"c\" manual=\"int%20counter%20%3D%20-1%3B%0Aint%20MAX_VALUE%20%3D%2016%3B%0A%0Avoid%20updateLED(int%20pin%2C%20int%20bit%2C%20int%20ctr)%20%7B%0A%20%20digitalWrite(pin%2C%20(ctr%20%26%20(1%3C%3Cbit))%20%3D%3D%20(1%3C%3Cbit)%20%3F%20HIGH%20%3A%20LOW)%3B%0A%7D%0Avoid%20setup()%20%7B%0A%20%20%2F%2F%20this%20runs%20once%0A%20%20pinMode(1%2C%20OUTPUT)%3B%0A%20%20pinMode(2%2C%20OUTPUT)%3B%0A%20%20pinMode(3%2C%20OUTPUT)%3B%0A%20%20pinMode(4%2C%20OUTPUT)%3B%0A%7D%0A%0Avoid%20loop()%20%7B%0A%20%20%2F%2F%20this%20runs%20repeatedly%0A%20%20counter%20%3D%20(counter%20%2B%201)%20%25%20MAX_VALUE%3B%0A%20%20updateLED(1%2C%200%2C%20counter)%3B%0A%20%20updateLED(2%2C%201%2C%20counter)%3B%0A%20%20updateLED(3%2C%202%2C%20counter)%3B%0A%20%20updateLED(4%2C%203%2C%20counter)%3B%0A%20%20delay(500)%3B%0A%7D\" message=\"\" highlight=\"\" provider=\"manual\"\/]","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3616,"url":"https:\/\/www.gubatron.com\/blog\/codejava-argb_8888-pixel-abstraction\/","url_meta":{"origin":3651,"position":1},"title":"[CODE\/JAVA] ARGB_8888 Pixel Abstraction","author":"gubatron","date":"December 4, 2016","format":false,"excerpt":"This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels. ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8's stand for the number of bits per channel. In Android, signed int's are used to represent pixel's alpha\/color information. Since Android's\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3388,"url":"https:\/\/www.gubatron.com\/blog\/how-to-create-a-list-that-holds-different-object-types-using-void-in-c\/","url_meta":{"origin":3651,"position":2},"title":"How to create a list that holds different object types using `void*` in C.","author":"gubatron","date":"April 5, 2015","format":false,"excerpt":"I remember being in school back around 1998 and not knowing enough about C to do this. After coding in other languages, then going back to C++ and understanding at a lower level how references and pointers work, this was pretty easy to figure out. In this exercise I store\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3956,"url":"https:\/\/www.gubatron.com\/blog\/accessing-and-manipulating-a-32bit-integer-as-an-array-in-c-thanks-to-union\/","url_meta":{"origin":3651,"position":3},"title":"Accessing and manipulating a 32bit integer as a byte array in C++ using unions","author":"gubatron","date":"November 12, 2021","format":false,"excerpt":"I don't think I've ever used union for anything, but today I came across a very interesting use case to avoid bit-shifting tricks when dealing with data embedded in numbers. What's a union? Microsoft defines it this way A\u00a0union\u00a0is a user-defined type in which all members share the same memory\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2568,"url":"https:\/\/www.gubatron.com\/blog\/bfs-vs-dfs-graph-search-algorithms-in-python\/","url_meta":{"origin":3651,"position":4},"title":"BFS vs DFS Graph Search Algorithms in Python","author":"gubatron","date":"November 15, 2011","format":false,"excerpt":"Here are implementations of iterative BFS and DFS search algorithms in Python. These are just to illustrate the slight difference in implementation of these algorithms. Basically, if you want to go deep, with DFS, you can use a queue on which you'll be adding the next elements to explore as\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3164,"url":"https:\/\/www.gubatron.com\/blog\/how-to-have-a-play-framework-app-autostart-during-boot-on-elastic-beanstalk-centos-ec2-instances\/","url_meta":{"origin":3651,"position":5},"title":"How to have a Play framework app autostart during boot on Elastic Beanstalk CentOS ec2 instances","author":"gubatron","date":"September 27, 2013","format":false,"excerpt":"So you've created an Elastic Beanstalk environment, you have a play framework distribution which you've created using play dist (either on your local environment, or right there on the server, whatever you prefer) play dist outputs a my-app-1.0.zip file which has a self-contained version of your app with all the\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3651"}],"version-history":[{"count":8,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3651\/revisions"}],"predecessor-version":[{"id":3780,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3651\/revisions\/3780"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}