<?
  /**
   * Display time remaining for the contestants to participate in the 1st DCS
   * Movie Maker Smack-Down: http://forums.eagle.ru/showthread.php?p=1245136
   *
   * Creates a simple static GIF image with the time remaining rendered into it.
   *
   * Created by Yurgon (0817 at yurgon dot net) around 2011-07-19.
   *
   * Feel free to use/copy/deploy this code for your own purposes or take it as
   * inspiration.
   *
   * Last modified 2011-07-20, 01:36 CEST
   */

  // There's a RewriteRule in place to rewrite dcs_moviemaker_countdown.gif to
  // index.php, so let's refuse direct requests.
  if (!preg_match("/\.gif$/", $_SERVER["REQUEST_URI"])) {
    header("HTTP/1.0 404 Not found");
    die("File not found");
  }

  $date_now = new DateTime("now", new DateTimeZone("UTC"));
  $date_end = new DateTime("2011-08-11, 23:59:59", new DateTimeZone("UTC"));

  if ($date_now > $date_end) {
    render_img("(Event passed)");
    exit;
  }

  /**
   * Oh, great. DateInterval is available in PHP 5.3+
   * Since this machine was running PHP 5.2.6 (Debian Lenny), I actually had to
   * upgrade to Squeeze. Well, at least it wasn't one of those "Oh, it's the end
   * of life again?" last minute distribution upgrades.
   */
  $intvl = $date_now->diff($date_end);

  if (($str = diff2string($intvl)) == "")
    $str = "(unknown)";

  render_img($str);
  exit;


  /**
   * The DateInterval has some nice properties that we can simply extract.
   */
  function diff2string ($intvl)
  {
    $units = array(
      "y" => "year",
      "m" => "month",
      "d" => "day",
      "h" => "hour",
      "i" => "minute",
      "s" => "second",
    );

    $out = array();
    foreach ($units as $key => $unit) {
      // Drop empty units, but avoid gaps like 5 days, 12 minutes
      // (e.g., if hours == 0, only ignore them if days AND months AND years
      // were all 0 as well).
      if ($intvl->$key == 0 && count($out) == 0)
        continue;

      if ($intvl->$key != 1)
        $unit .= "s";

      $out[] = $intvl->$key . " " . $unit;
    }
    return implode(", ", $out);
  }

  /**
   * Finally, let's render the text into an image. For reasons of being lazy,
   * I photoshopped an empty GIF with the background color used on the ED forums
   * so all that's left to do is to put the text in it and then send the file to
   * the browser.
   */
  function render_img($str)
  {
    // Yeah, not really hardened error handling, but should be enough for today's
    // purposes.
    if (!$im = imagecreatefromgif("./empty.gif")) {
      header("HTTP/1.0 500 Internal Server Error");
      die();
    }

    $black = imagecolorallocate($im, 0, 0, 0);
    imagestring($im, 4, 0, 3, $str, $black);

    // Works, but has ugly antialiasing effects. Besides, might have copyright
    // issues because of the font.
    //putenv('GDFONTPATH=' . realpath('.'));
    //$font_file = "verdana.ttf";
    //imagettftext($im, 10, 0, 0, 17, -$black, $font_file, $str);

    header("Content-Type: image/gif");
    imagegif($im);
    imagedestroy($im);
  }
?>