不用GD库生成当前时间的PNG格式图象的程序

该程序是不用GD库可以生成当前时间的PNG格式图象,给人大开眼界,很有参考价值. teaman整理

  1   
  2  
  3function set_4pixel($r, $g, $b, $x, $y)   
  4{   
  5global $sx, $sy, $pixels; 
  6
  7$ofs = 3 * ($sx * $y + $x);   
  8$pixels[$ofs] = chr($r);   
  9$pixels[$ofs + 1] = chr($g);   
 10$pixels[$ofs + 2] = chr($b);   
 11$pixels[$ofs + 3] = chr($r);   
 12$pixels[$ofs + 4] = chr($g);   
 13$pixels[$ofs + 5] = chr($b);   
 14$ofs += 3 * $sx;   
 15$pixels[$ofs] = chr($r);   
 16$pixels[$ofs + 1] = chr($g);   
 17$pixels[$ofs + 2] = chr($b);   
 18$pixels[$ofs + 3] = chr($r);   
 19$pixels[$ofs + 4] = chr($g);   
 20$pixels[$ofs + 5] = chr($b);   
 21}   
 22//生成数字图象的函数   
 23function draw2digits($x, $y, $number)   
 24{   
 25draw_digit($x, $y, (int) ($number / 10));   
 26draw_digit($x + 11, $y, $number % 10);   
 27}   
 28  
 29function draw_digit($x, $y, $digit)   
 30{   
 31global $sx, $sy, $pixels, $digits, $lines;   
 32  
 33$digit = $digits[$digit];   
 34$m = 8;   
 35for ($b = 1, $i = 0; $i < 7; $i++, $b *= 2) {   
 36if (($b & $digit) == $b) {   
 37$j = $i * 4;   
 38$x0 = $lines[$j] * $m + $x;   
 39$y0 = $lines[$j + 1] * $m + $y;   
 40$x1 = $lines[$j + 2] * $m + $x;   
 41$y1 = $lines[$j + 3] * $m + $y;   
 42if ($x0 == $x1) {   
 43$ofs = 3 * ($sx * $y0 + $x0);   
 44for ($h = $y0; $h <= $y1; $h++, $ofs += 3 * $sx) {   
 45$pixels[$ofs] = chr(0);   
 46$pixels[$ofs + 1] = chr(0);   
 47$pixels[$ofs + 2] = chr(0);   
 48}   
 49} else {   
 50$ofs = 3 * ($sx * $y0 + $x0);   
 51for ($w = $x0; $w <= $x1; $w++) {   
 52$pixels[$ofs++] = chr(0);   
 53$pixels[$ofs++] = chr(0);   
 54$pixels[$ofs++] = chr(0);   
 55}   
 56}   
 57}   
 58}   
 59}   
 60  
 61//将文字加入到图象中   
 62function add_chunk($type)   
 63{   
 64global $result, $data, $chunk, $crc_table; 
 65
 66// chunk :为层   
 67// length: 4 字节: 用来计算 chunk   
 68// chunk type: 4 字节   
 69// chunk data: length bytes   
 70// CRC: 4 字节: 循环冗余码校验   
 71  
 72// copy data and create CRC checksum   
 73$len = strlen($data);   
 74$chunk = pack("c*", ($len >> 24) & 255,   
 75($len >> 16) & 255,   
 76($len >> 8) & 255,   
 77$len & 255);   
 78$chunk .= $type;   
 79$chunk .= $data; 
 80
 81// calculate a CRC checksum with the bytes chunk[4..len-1]   
 82$z = 16777215;   
 83$z |= 255 << 24;   
 84$c = $z;   
 85for ($n = 4; $n < strlen($chunk); $n++) {   
 86$c8 = ($c >> 8) & 0xffffff;   
 87$c = $crc_table[($c ^ ord($chunk][$n])) & 0xff] ^ $c8;   
 88}   
 89$crc = $c ^ $z; 
 90
 91$chunk .= chr(($crc >> 24) & 255);   
 92$chunk .= chr(($crc >> 16) & 255);   
 93$chunk .= chr(($crc >> 8) & 255);   
 94$chunk .= chr($crc & 255); 
 95
 96// 将结果加到$result中   
 97$result .= $chunk;   
 98} 
 99
100  
101//主程序 
102
103$sx = 80;   
104$sy = 21;   
105$pixels = "";   
106  
107// 填充   
108for ($h = 0; $h < $sy; $h++) {   
109for ($w = 0; $w < $sx; $w++) {   
110$r = 100 / $sx * $w + 155;   
111$g = 100 / $sy * $h + 155;   
112$b = 255 - (100 / ($sx + $sy) * ($w + $h));   
113$pixels .= chr($r);   
114$pixels .= chr($g);   
115$pixels .= chr($b);   
116}   
117}   
118  
119$date = getdate();   
120$s = $date["seconds"];   
121$m = $date["minutes"];   
122$h = $date["hours"];   
123$digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);   
124$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);   
125  
126draw2digits(4, 2, $h);   
127draw2digits(30, 2, $m);   
128draw2digits(56, 2, $s);   
129set_4pixel(0, 0, 0, 26, 7);   
130set_4pixel(0, 0, 0, 26, 13);   
131set_4pixel(0, 0, 0, 52, 7);   
132set_4pixel(0, 0, 0, 52, 13); 
133
134// 创建循环冗余码校验表   
135$z = -306674912; // = 0xedb88320   
136for ($n = 0; $n < 256; $n++) {   
137$c = $n;   
138for ($k = 0; $k < 8; $k++) {   
139$c2 = ($c >> 1) & 0x7fffffff;   
140if ($c & 1) $c = $z ^ ($c2); else $c = $c2;   
141}   
142$crc_table[$n] = $c;   
143} 
144
145// PNG file signature   
146$result = pack("c*", 137,80,78,71,13,10,26,10);   
147  
148// IHDR chunk data:   
149// width: 4 bytes   
150// height: 4 bytes   
151// bit depth: 1 byte (8 bits per RGB value)   
152// color type: 1 byte (2 = RGB)   
153// compression method: 1 byte (0 = deflate/inflate)   
154// filter method: 1 byte (0 = adaptive filtering)   
155// interlace method: 1 byte (0 = no interlace)   
156$data = pack("c*", ($sx >> 24) & 255,   
157($sx >> 16) & 255,   
158($sx >> 8) & 255,   
159$sx & 255,   
160($sy >> 24) & 255,   
161($sy >> 16) & 255,   
162($sy >> 8) & 255,   
163$sy & 255,   
1648,   
1652,   
1660,   
1670,   
1680);   
169add_chunk("IHDR"); 
170
171// 以下不敢乱翻译,请自行参考   
172// scanline:   
173// filter byte: 0 = none   
174// RGB bytes for the line   
175// the scanline is compressed with "zlib", method 8 (RFC-1950):   
176// compression method/flags code: 1 byte ($78 = method 8, 32k window)   
177// additional flags/check bits: 1 byte ($01: FCHECK = 1, FDICT = 0, FLEVEL = 0)   
178// compressed data blocks: n bytes   
179// one block (RFC-1951):   
180// bit 0: BFINAL: 1 for the last block   
181// bit 1 and 2: BTYPE: 0 for no compression   
182// next 2 bytes: LEN (LSB first)   
183// next 2 bytes: one's complement of LEN   
184// LEN bytes uncompressed data   
185// check value: 4 bytes (Adler-32 checksum of the uncompressed data)   
186//   
187$len = ($sx * 3 + 1) * $sy;   
188$data = pack("c*", 0x78, 0x01,   
1891,   
190$len & 255,   
191($len >> 8) & 255,   
192255 - ($len & 255),   
193255 - (($len >> 8) & 255));   
194$start = strlen($data);   
195$i2 = 0;   
196for ($h = 0; $h < $sy; $h++) {   
197$data .= chr(0);   
198for ($w = 0; $w < $sx * 3; $w++) {   
199$data .= $pixels[$i2++];   
200}   
201} 
202
203  
204// calculate a Adler32 checksum with the bytes data[start..len-1]   
205$s1 = 1;   
206$s2 = 0;   
207for ($n = $start; $n < strlen($data); $n++) {   
208$s1 = ($s1 + ord($data[$n])) % 65521;   
209$s2 = ($s2 + $s1) % 65521;   
210}   
211$adler = ($s2 << 16) | $s1; 
212
213$data .= chr(($adler >> 24) & 255);   
214$data .= chr(($adler >> 16) & 255);   
215$data .= chr(($adler >> 8) & 255);   
216$data .= chr($adler & 255);   
217add_chunk("IDAT"); 
218
219// IEND: marks the end of the PNG-file   
220$data = "";   
221add_chunk("IEND"); 
222
223// 列印图象   
224  
225echo($result);   

//如何调用,其实很简单,将上面存为Timeimg.php
//然后新建一个页面如下:

1<html>
2<head>
3<title>test</title>
4<meta content="text/html" http-equiv="Content-Type"/>
5</head>
6<body>
7<img src="Timeimg.php"/> //以图象连接方式调用PHP文件   
8</body>
9</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus