home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / php.exe / Clock.php < prev    next >
Encoding:
PHP Script  |  2001-07-02  |  7.5 KB  |  255 lines

  1.     //**************************************
  2.     //     
  3.     // Name: Clock
  4.     // Description:Shows the current time as
  5.     //     a PNG-image. This script does not use th
  6.     //     e GD library. All PNG generation code, i
  7.     //     ncluding CRC checksum calculation and ZL
  8.     //     IB-implementation, is pure PHP3. You can
  9.     //     use it as a benchmark (because it's slow
  10.     //     ), or as a quick reference for implement
  11.     //     ing a simple PNG-file generator. by Fran
  12.     //     k Bu▀ on
  13.     // By: PHP Code Exchange
  14.     //**************************************
  15.     //     
  16.     
  17.     <?php
  18.     //
  19.     // clock.php3 -- shows the current time 
  20.     //     as a PNG-image
  21.     // Copyright (C) 2000, Frank Bu▀, fb@fra
  22.     //     nk-buss.de
  23.     //
  24.     // This software is provided 'as-is', wi
  25.     //     thout any express or implied
  26.     // warranty. In no event will the author
  27.     //     be held liable for any damages
  28.     // arising from the use of this software
  29.     //     .
  30.     //
  31.     // Permission is granted to anyone to us
  32.     //     e this software for any purpose,
  33.     // including commercial applications, an
  34.     //     d to alter it and redistribute it
  35.     // freely.
  36.     //
  37.     function set_4pixel($r, $g, $b, $x, $y)
  38.     {
  39.         global $sx, $sy, $pixels;
  40.         $ofs = 3 * ($sx * $y + $x);
  41.         $pixels[$ofs] = chr($r);
  42.         $pixels[$ofs + 1] = chr($g);
  43.         $pixels[$ofs + 2] = chr($b);
  44.         $pixels[$ofs + 3] = chr($r);
  45.         $pixels[$ofs + 4] = chr($g);
  46.         $pixels[$ofs + 5] = chr($b);
  47.         $ofs += 3 * $sx;
  48.         $pixels[$ofs] = chr($r);
  49.         $pixels[$ofs + 1] = chr($g);
  50.         $pixels[$ofs + 2] = chr($b);
  51.         $pixels[$ofs + 3] = chr($r);
  52.         $pixels[$ofs + 4] = chr($g);
  53.         $pixels[$ofs + 5] = chr($b);
  54.     }
  55.     function draw2digits($x, $y, $number)
  56.     {
  57.         draw_digit($x, $y, (int) ($number / 10));
  58.         draw_digit($x + 11, $y, $number % 10);
  59.     }
  60.     function draw_digit($x, $y, $digit)
  61.     {
  62.         global $sx, $sy, $pixels, $digits, $lines;
  63.         $digit = $digits[$digit];
  64.         $m = 8;
  65.         for ($b = 1, $i = 0; $i < 7; $i++, $b *= 2) {
  66.         if (($b & $digit) == $b) {
  67.             $j = $i * 4;
  68.             $x0 = $lines[$j] * $m + $x;
  69.             $y0 = $lines[$j + 1] * $m + $y;
  70.             $x1 = $lines[$j + 2] * $m + $x;
  71.             $y1 = $lines[$j + 3] * $m + $y;
  72.             if ($x0 == $x1) {
  73.             $ofs = 3 * ($sx * $y0 + $x0);
  74.             for ($h = $y0; $h <= $y1; $h++, $ofs += 3 * $sx) {
  75.                 $pixels[$ofs] = chr(0);
  76.                 $pixels[$ofs + 1] = chr(0);
  77.                 $pixels[$ofs + 2] = chr(0);
  78.             }
  79.             } else {
  80.             $ofs = 3 * ($sx * $y0 + $x0);
  81.             for ($w = $x0; $w <= $x1; $w++) {
  82.                 $pixels[$ofs++] = chr(0);
  83.                 $pixels[$ofs++] = chr(0);
  84.                 $pixels[$ofs++] = chr(0);
  85.             }
  86.             }
  87.         }
  88.         }
  89.     }
  90.     // create a chunk with the bytes in $dat
  91.     //     a and the specified
  92.     // type and add it to $result
  93.     function add_chunk($type)
  94.     {
  95.         global $result, $data, $chunk, $crc_table;
  96.         // chunk layout:
  97.         // length: 4 bytes: counting chunk data only
  98.         // chunk type: 4 bytes
  99.         // chunk data: length bytes
  100.         // CRC: 4 bytes: CRC-32 checksum of the type and the data
  101.         
  102.         // copy data and create CRC checksum
  103.         $len = strlen($data);
  104.         $chunk = pack("c*", ($len >> 24) & 255,
  105.             ($len >> 16) & 255,
  106.             ($len >> 8) & 255,
  107.             $len & 255);
  108.         $chunk .= $type;
  109.         $chunk .= $data;
  110.         // calculate a CRC checksum with the bytes chunk[4..len-1]
  111.         $z = 16777215;
  112.         $z |= 255 << 24;
  113.         $c = $z;
  114.         for ($n = 4; $n < strlen($chunk); $n++) {
  115.         $c8 = ($c >> 8) & 0xffffff;
  116.         $c = $crc_table[($c ^ ord($chunk[$n])) & 0xff] ^ $c8;
  117.         }
  118.         $crc = $c ^ $z;
  119.         $chunk .= chr(($crc >> 24) & 255);
  120.         $chunk .= chr(($crc >> 16) & 255);
  121.         $chunk .= chr(($crc >> 8) & 255);
  122.         $chunk .= chr($crc & 255);
  123.         // add it to the result
  124.         $result .= $chunk;
  125.     }
  126.     // ============
  127.     // main program
  128.     // ============
  129.     $sx = 80;
  130.     $sy = 21;
  131.     $pixels = "";
  132.     // create filling
  133.     for ($h = 0; $h < $sy; $h++) {
  134.         for ($w = 0; $w < $sx; $w++) {
  135.         $r = 100 / $sx * $w + 155;
  136.         $g = 100 / $sy * $h + 155;
  137.         $b = 255 - (100 / ($sx + $sy) * ($w + $h));
  138.         $pixels .= chr($r);
  139.         $pixels .= chr($g);
  140.         $pixels .= chr($b);
  141.         }
  142.     }
  143.         
  144.     $date = getdate();
  145.     $s = $date["seconds"];
  146.     $m = $date["minutes"];
  147.     $h = $date["hours"];
  148.     $digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);
  149.     $lines = array(1, 1, 1, 2, 0, 1, 0, 2, 1, 0, 1, 1, 0, 0, 0, 1, 0, 2, 1, 2, 0, 1, 1, 1, 0, 0, 1, 0);
  150.         
  151.     draw2digits(4, 2, $h);
  152.     draw2digits(30, 2, $m);
  153.     draw2digits(56, 2, $s);
  154.     set_4pixel(0, 0, 0, 26, 7);
  155.     set_4pixel(0, 0, 0, 26, 13);
  156.     set_4pixel(0, 0, 0, 52, 7);
  157.     set_4pixel(0, 0, 0, 52, 13);
  158.     // create crc-table
  159.     $z = -306674912; // = 0xedb88320
  160.     for ($n = 0; $n < 256; $n++) {
  161.     $c = $n;
  162.     for ($k = 0; $k < 8; $k++) {
  163.     $c2 = ($c >> 1) & 0x7fffffff;
  164.     if ($c & 1) $c = $z ^ ($c2); else $c = $c2;
  165.     }
  166.     $crc_table[$n] = $c;
  167.     }
  168.     // PNG file signature
  169.     $result = pack("c*", 137,80,78,71,13,10,26,10);
  170.         
  171.     // IHDR chunk data:
  172.     //width: 4 bytes
  173.     //height: 4 bytes
  174.     //bit depth: 1 byte (8 bits per RGB valu
  175.     //     e)
  176.     //color type: 1 byte (2 = RGB)
  177.     //compression method: 1 byte (0 = deflat
  178.     //     e/inflate)
  179.     //filter method: 1 byte (0 = adaptive fi
  180.     //     ltering)
  181.     //interlace method:1 byte (0 = no interl
  182.     //     ace)
  183.     $data = pack("c*", ($sx >> 24) & 255,
  184.         ($sx >> 16) & 255,
  185.         ($sx >> 8) & 255,
  186.         $sx & 255,
  187.         ($sy >> 24) & 255,
  188.         ($sy >> 16) & 255,
  189.         ($sy >> 8) & 255,
  190.         $sy & 255,
  191.         8,
  192.         2,
  193.         0,
  194.         0,
  195.         0);
  196.     add_chunk("IHDR");
  197.     // IDAT: image data:
  198.     //scanline:
  199.     //filter byte: 0 = none
  200.     //RGB bytes for the line
  201.     //the scanline is compressed with "zlib"
  202.     //     , method 8 (RFC-1950):
  203.     //compression method/flags code: 1 byte 
  204.     //     ($78 = method 8, 32k window)
  205.     //additional flags/check bits:1 byte ($0
  206.     //     1: FCHECK = 1, FDICT = 0, FLEVEL = 0)
  207.     //compressed data blocks:n bytes
  208.     //one block (RFC-1951):
  209.     //bit 0: BFINAL: 1 for the last block
  210.     //bit 1 and 2: BTYPE: 0 for no compressi
  211.     //     on
  212.     //next 2 bytes: LEN (LSB first)
  213.     //next 2 bytes: one's complement of LEN
  214.     //LEN bytes uncompressed data
  215.     //check value: 4 bytes (Adler-32 checksu
  216.     //     m of the uncompressed data)
  217.     //
  218.     $len = ($sx * 3 + 1) * $sy;
  219.     $data = pack("c*", 0x78, 0x01,
  220.     1,
  221.         $len & 255,
  222.         ($len >> 8) & 255,
  223.         255 - ($len & 255),
  224.         255 - (($len >> 8) & 255));
  225.     $start = strlen($data);
  226.     $i2 = 0;
  227.     for ($h = 0; $h < $sy; $h++) {
  228.         $data .= chr(0);
  229.         for ($w = 0; $w < $sx * 3; $w++) {
  230.         $data .= $pixels[$i2++];
  231.         }
  232.     }
  233.     // calculate a Adler32 checksum with the
  234.     //     bytes data[start..len-1]
  235.     $s1 = 1;
  236.     $s2 = 0;
  237.     for ($n = $start; $n < strlen($data); $n++) {
  238.         $s1 = ($s1 + ord($data[$n])) % 65521;
  239.         $s2 = ($s2 + $s1) % 65521;
  240.     }
  241.     $adler = ($s2 << 16) | $s1;
  242.     $data .= chr(($adler >> 24) & 255);
  243.     $data .= chr(($adler >> 16) & 255);
  244.     $data .= chr(($adler >> 8) & 255);
  245.     $data .= chr($adler & 255);
  246.     add_chunk("IDAT");
  247.     // IEND: marks the end of the PNG-file
  248.     $data = "";
  249.     add_chunk("IEND");
  250.     // print the image
  251.     header("Content-type: image/png");
  252.     print($result);
  253.     ?>
  254.  
  255.