WMLPrograming list

7.5 Can I generate dynamic WBMP images on-the-fly?

Of course. You will, like so very often, need some form of server side script language like PHP or ASP or Perl to do it. In addition, you'll most likely need some form of image converter because few server side script languages can generate WBMP images by themselves.

How it's all done depends on the script language, but here's a PHP example which will generate the a WBMP image.

The image above is created by GD which is compiled into PHP. Note that current versions of GD do no longer create GIF images, but so called PNG (Portable Network Graphics) images. Small parts of the code below needs to be changed from GIF to PNG if you're using the current version of PHP. In addition, and what does the conversion from either PNG or GIF to WBMP, is DuPont's Image Magick which converts between practically any thinkable format. It is available for most platforms.

In the code, I've used the PHP function ImageCreateFromGif() because I wanted to put some text on the GIF image. You can generate a blank GIF/PNG image with PHP's ImageGreate() function instead.

And now for the code:

  // hardcoded "variables" are safer!
  $blank = "./wapclock_blank.gif";                              // path to blank GIF file - not really needed (see above)
                                                                // Look at it at ....
  $input = "/tmp/wapclock.gif";                                 // path to input file generated by PHP
  $output = "/tmp/wapclock.out";                                // path to temporary output file. Extension is irrelevant
  $convert = "/usr/local/bin/convert";                          // path to ImageMagick convert
  if($format == "gif") {
     header("Content-type: image/gif");                         // if it's GIF, send that Content-type
     $type = "GIF";
  else {
    if($format == "wbmp") {                                     // if it's WBMP, send that Content-type
      header("Content-type: image/vnd.wap.wbmp");
      $type = "WBMP";
    else {
      header("Content-type: text/plain");                       // hope that the browser can read this
      $type = "";                                               // or someone forgot to set the format variable
  header("Expires:  05:00:00 GMT");             // turn off caching
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header("Cache-Control: no-cache, must-revalidate");
  header("Pragma: no-cache");
  $im = imagecreatefromgif($blank);                             // create a GIF file from an empty GIF file (see faq)
  $time = date("H:m:s");                                        // put the current time into the time variable
  imagestring($im,4,6,15,$time,0);                              // Place time variable sort of in the middle, with font size 4
  ImageGif($im,$input);                                         // generate a GIF file with PHP (see faq)
  ImageDestroy($im);                                            // empty the GD temporary buffer
  if(strlen($type) > 0) {                                       // if the type is known
    exec($convert." ".$input." ".$type.":".$output);            // do the conversion
  else {
    echo("Unknown format!\n");                                  // or do nothing
    exit;                                                       // and stop
  $fd = fopen($output,"r");                                     // open the converted file
  $contents = fread($fd,filesize($output));                     // read eveything into a variable
  fclose($fd);                                                  // close the file
  echo($contents);                                              // pour out the contents

All the code above does is output an either GIF of WBMP data data stream. To use the actual image in a WAP application, you'll have to do something like this:

  header("Content-type: text/vnd.wap.wml");
  echo("<?xml version=\"1.0\"?>\n");
  echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://..\">\n\n");
  echo("<!-- Code written in Microsoft NOTEPAD.EXE. (c)  Color Line ASA -->\n");
  <card id="wapclock" title="WAP Clock">
    <do type="prev" label="Reload Image">
      <go href="<?echo($PHP_SELF)?>"/>
      <img src="" alt="You should see an image..."/>

This WAP application is, like all the others, available in the "Demos" section here: DEMO.

Related information

WAP Shareware's Software Directory

[ Main ]   [ 07 - Making it look fancy ]