28
Sep
2

Google chart over HTTPS/SSL

The google charts API does not support the https protocol. If your website is being delivered through a secure connection, the chart will cause a SSL error. Here's a quick way to deliver google chart images over ssl.

To start off with, the chart image must be delivered from a secure connection. Google doesn't allow this plain and simple, so we need to figure out how to host it from our own site. We accomplish this by fetching the image from google using the standard API, writing it to a file, and then calling it on our own script. We basically make a image handling proxy.

Let's take a simple google chart to experiment with.

PHP:
  1. $chart_image = 'http://chart.apis.google.com/chart?chs=500x50&chf=bg,s,ffffff&cht=ls&chd=t:23.52,20.58,26.47,23.52,23.52,23.52,100.00,0.00,23.52,23.52,27.94,20.58,23.52&chco=0066ff';

Next we need to make a function to fetch and save the google chart locally. It will check the chart against the local copy and save it if the chart doesn't exist, or the image has changed. This way we aren't re-writing the same chart on every request, but if the chart changes, it will be updated appropriately.

PHP:
  1. public static function saveImage($chart_url,$path,$file_name){
  2.             if(!file_exists($path.$file_name) || (md5_file($path.$file_name) != md5_file($chart_url)))
  3.             {
  4.                 file_put_contents($path.$file_name,file_get_contents($chart_url));
  5.             }
  6.  
  7.             return $file_name;
  8.     }

Lastly we tie it all together so that it is usable in our application. Im using this within a class, but this could just be used as a function as well. Your image directory will need to be writable for this to work.

PHP:
  1. public function doSomething()
  2. {
  3.  
  4. $local_image_path = '/path/to/images/charts/';
  5. $image_name = 'some_chart_image.png';
  6. $chart_url = 'http://chart.apis.google.com/chart?chs=500x50&chf=bg,s,ffffff&cht=ls&chd=t:23.52,20.58,26.47,23.52,23.52,23.52,100.00,0.00,23.52,23.52,27.94,20.58,23.52&chco=0066ff';
  7.  
  8. $image = self::saveImage($chart_url ,$local_image_path,$image_name);
  9.  
  10. }

You'll need to implement your own error handling, and adjust this to meet the paths and specifics of your server, but the image can now be called from:
<img src="/images/charts/some_chart_image.png" alt="" />

If you need help creating your base chart image, this tool is a great place to start.

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
2 Comments:
  1. mikosh 1 Dec, 2009

    Thank you! That’s what i was looking for! i didn’t need to use https, but i needed to grab image chart and then include in pdf (with fpdf). Your trick works great.

    $chart_image_url=’http://chart.apis.google.com/chart?…’;
    $local_charts_path = ‘../img/charts/’;
    $chart_image_name = ‘chart.png’; file_put_contents($local_charts_path.$chart_image_name,file_get_contents($chart_image_url));

    then, after:

    $pdf->Image($local_charts_path.$chart_image_name,10,10,120,20);

  2. Brandon 5 Jan, 2010

    Great code!! Exactly what I needed. Thank you for posting for everyone use.

Post your comment



Copyright © 2010 SayNoToFlash, Jamie Estep, All Rights Reserved · Theme design by Themes Boutique