Trick: Use your own web bug images

From BitWise DocuWiki

Originally posted by Skraggy in the Tips & Tricks forum circa April 2004

I've created a PHP script to display my various IM web bugs and figured I'd throw out here the bitwise piece to show how you can use your own web bugs instead of the ones available in the event it's helpful to anyone. This requires the Bitwise image to be set to the ones with just text in the image (otherwise you will have to calculate your own crc values). The code is very basic, but effective.

<?php
if ($bitwise) {
  header("Content-type: image/gif");
  $img = file("http://www.bitwiseim.com/users/webBug.php?id=$bitwise");
  $status = crc32($img[0]);
  switch ($status) {
     case "306575361":
        readfile("images/bwonline.gif");
        break;
     case "-1426359039":
        readfile("images/bwaway.gif");
        break;
     case "1924638085":
     default:
        readfile("images/bwoffline.gif");
  }
?>

Put your own images in an images subdirectory below where the script is running, then you can create an image reference that uses the URL to your script .../bwstatus.php?bitwise=ID where ID is your numerical ID for the web bug (see your online preference settings). If you use different image names and types, the changes required should be relatively obvious.

An updated advanced version was also posted

I've rewritten my status program to be more robust.

Added in this rewrite: Caching (result returns the same for 2 minutes before refreshing cache) and timeouts (times out by default after 5 seconds). It also just redirects the browser to the correct image instead of trying to read the image itself (for more flexibility). The primary reason for these changes is that I use my status icons mostly as my forum signature, and I wanted as quick response as reasonably possible.

Due to the changes, this new version requires phpCache (free download) and cURL (also free) support compiled into PHP.

I'm new to PHP so I don't claim the code is perfect. If you use other IM's, I also have a version (the one I use) that includes support for AIM, Yahoo, ICQ, and BitWise currently. That can be found here.

Btw, for consistancy, I don't differentiate between online and away (because the other IM's don't). You can change that in the code rather easily if you desire.

<?php
/*
  Bitwise Status v1.0 - Allow use of custom status icons
  Copyright (C) 2004 Michael Jones (Skraggy) - mike@biggorilla.com

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  <http://www.gnu.org/licenses/gpl.txt>

  -----

  Requires phpCache <http://www.0x00.org/php/phpCache/>
    Comment out the line "ob_end_flush();" in phpCache.inc so that headers don't get
    sent prematurely (Line 249 in phpCache v1.4)
    Depending on your environment, you may also want to change CACHE_DIR.

  Requires PHP to have CURL support compiled in.  Check your PHP configuration.
*/

ini_set("display_errors","0"); // Set to 1 for debugging

$imgurl = "http://yourdomain.com/images/imstatus/"; // Location of default images

include("phpCache/phpCache.inc");

function get_status($url) {
  $res = curl_init();

  curl_setopt($res, CURLOPT_URL, $url);
  curl_setopt($res, CURLOPT_HEADER, 0);
  curl_setopt($res, CURLOPT_TIMEOUT, 5);
  curl_setopt($res, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($res, CURLOPT_FOLLOWLOCATION, 1);

  return curl_exec($res);

  curl_close($res);
}

if ($id) {
  if (!$on) $on = $imgurl."bwonline.gif";
  if (!$off) $off = $imgurl."bwoffline.gif";

  if (!($et=cache_all(120))) { // Cache result for 2 minutes
     $image = get_status("http://www.bitwiseim.com/users/webBug.php?id=$id");
     $status = crc32($image);
     cache_variable("status");
     endcache();
  }

  // CRC values based on text-only Bitwise status icons (see your BW online preferences)
  switch ($status) {
     case "-1496083650": // Online
     case "-1796816539": // Away
        header("Location: $on");
        break;
     default: // Error or Offline
        header("Location: $off");
  }
}

?>