Er...somethin' like that.

Sunday, May 27, 2007

Terror! (Or is that "Terr"?)

So, even further back, an entire blog ago, in fact, I hacked together a little PHP script to display your own version of the government's terror alert level. I had it up on Dan Pourhadi's site for a while, until he switched hosts or something and lost it. It sat in the Software Projects folder on my desktop, gathering virtual dust. About a week ago, I had been playing around with regexes and rediscovered this gem, and set about to rewriting it, basically from the ground up.

The changes:
  • 1 regular expression performed, instead of 2 in the best case, and 6 in the worst case.
  • file_get_contents instead of implode()-ing a file()
  • A simple switch() construct instead of a chain of if/elseifs with regexes
  • Optimized the order of the switch cases by sorting them by how likely they are to come up
So it's optimized quite a bit, and works wonderfully. Then, just today, I made a rather interesting discovery. Some time ago, Mivox used my code (with attribution!) to produce her own version with her own graphics, and used the GD library in PHP to allow the script to output an image directly. That allows you to reference the PHP script as the src of an img tag, instead of having to include the script.
I found that to be a fabulous idea, so after downloading Xcode Tools to compile GD to test it out, I implemented that change in my new version of the script. So:

Terror.php, Include Version
  • To be included using a PHP include()
  • Runs on a server with only a PHP installation
  • May be slightly faster
Terror.php, Image Version
  • To be referenced from an HTML img tag
  • Runs on a server with PHP and the GD image library installed
  • Much easier to reference, especially from outside
  1. Download the script of your choice
  2. Upload it to the desired location on your PHP-compatible server. Remember, the Image Version requires the GD library installed on your server
  3. In the same directory, upload PNG format images:
    1. severe.png
    2. high.png
    3. elevated.png
    4. guarded.png
    5. low.png
    6. (Image Version only) error.png
Then, just reference the script in your pages:
Include Version:
<?php include("terror.php"); ?>

Image Version:
<img src="terror.php">

As the comments in the scripts say, if you use the script on your site, please email me and let me know; my email address is in the comments.

In the near future, I may be posting a few image sets for use with these scripts, since I see that Mivox is a fan of the (incomplete) Dragon Tales set.

1 comment:

mivox said...

haha! Just found this post ... thanks for the reverse attribution. ;-)