Latest sites

Quick news

  • 2014-03-27: MongoDB Watchdog module ported to Drupal 8 at the Szeged Dev Days.
  • 2014-01-26: My post on the Symfony web profiler in Silex selected in Week of Symfony. w00t !
  • 2013-10-18: My first commit went into MongoDB today. And, guess what ? It's in JavaScript
  • 2013-09-20 to 29: Working on Drupal 8 EntityAPI at the extended code sprints during and around DrupalCon Prague
  • 2012-08-19: Working on Drupal 8 EntityAPI at Drupalcon Munich
  • 2012-06-15: Working on Drupal 8 EntityAPI at DrupalDevDays Barcelona
  • 2012-03-23: Working on the future Drupal Document Oriented Storage at DrupalCon Denver. D8 or later ? Bets are on Later

Live graphical stats for php-gtk.eu with Drupal

The PHP-GTK community site has been running on Drupal 5 betas since its creation one month ago, and I thought it really needed a live chart of statistics to match its glorious Garland dress, and GD would allow it. But how does one go about creating charts within Drupal ?

PHP-GTK graphical stats.

The image above is the live image I'm describing in this post.

Inserting the image link

Actually there's nothing special to that part. Supposing a page, say http://www.php-gtk.eu/site/access-stats needs a graph of some data, in this case Drupal stats taken from the accesslog table. All it takes is a piece of code in a module generating the image, and a simple img element in the page source, pointing to that dynamically generated image.

In this case, the link is a plain <img src="http://php-gtk.eu/gtkphpnet/stats.png" alt="PHP-GTK graphical stats" />, the path to which lets us guess it is actually handled by the gtkphpnet module.

Creating the image in a module

The interesting part is this one: how does one go about creating a live image within drupal ? After all, Drupal themes output, and image files have to be exact binary representations, served with the proper content type.

Going full-page

The only trick here is to avoid Drupal serving the rest of the page, before and after the output of our module.

Looking at the page-serving mechanism, Drupal obtains module output in order to theme it from the menu system : it invokes modules from the callback parameter in menu items within hook_menu implementations. When it obtains the output from the module, it proceeds to theme it.

Now, if the module never returns, Drupal won't receive the output, and won't theme it. The screen output will be exactly what our module has emitted (and not what it has returned , as ought to be the case for a normal themable output).

Telling the browser the content is an image

From then on, everything is just plain GD. The only notable part if the fact that the module must define the content type to match the format of the image being generated, otherwise the default text/html will be used, and not all browsers will recognize the image for what it really is.

The syntax is simple. just use the Drupal header wrapper: drupal_set_header, like:

<?php
  drupal_set_header
("Content-type: image/png");
?>

making sure that the type you'll be sending matches the format of the image you are generating. Some browsers, like Opera and MSIE, actually override the headers if they recognize the format, but Firefox 1.5 uses the content-type it receives if one is available, causing erroneous displays looking like PNG or GIF89a followed by junk.

The documentation for the GD functions is available on PHP.net at http://www.php.net/gd.

Available module

Great work here! Is it posible to make your module available? I´d love to implement this on my site.

Great idea!
rlbravo

Obtaining data and plotting

The request to obtain the data is just:

    SELECT
      DATE(FROM_UNIXTIME(al.timestamp)) ladate,
      count(DATE(FROM_UNIXTIME(al.timestamp))) cnt
    FROM {accesslog} al
    GROUP BY
      DATE(FROM_UNIXTIME(al.timestamp))';

As to the plotting code, you can just use the code at http://www.phpbuilder.com/columns/wiesendanger20001218.php3?page=6, replacing the

<?php
$data
= array( [...]
?>

part with the results from your query.

Since their example is directly usable, I think it's better for you to obtain it from them. My module is different, but has been inspired by it to a large extent, so I think I must refrain from publishing it, to avoid a potential copyright claim.

Thanks

That´s quite enough information. In fact, i´ve got it working! Thanks, great idea! rlbravo