A program that generates PNG format images for the current time without the GD library

Source: Internet
Author: User
Tags filter date format array ord pack php file strlen
Program This program is not the GD library can generate the current time of the PNG format image, to the people's eyes, very valuable reference. Teaman Finishing

<?php

function Set_4pixel ($r, $g, $b, $x, $y)
{
Global $SX, $sy, $pixels;

$ofs = 3 * ($SX * $y + $x);
$pixels [$ofs] = Chr ($r);
$pixels [$ofs + 1] = Chr ($g);
$pixels [$ofs + 2] = Chr ($b);
$pixels [$ofs + 3] = Chr ($r);
$pixels [$ofs + 4] = Chr ($g);
$pixels [$ofs + 5] = Chr ($b);
$ofs + + 3 * $SX;
$pixels [$ofs] = Chr ($r);
$pixels [$ofs + 1] = Chr ($g);
$pixels [$ofs + 2] = Chr ($b);
$pixels [$ofs + 3] = Chr ($r);
$pixels [$ofs + 4] = Chr ($g);
$pixels [$ofs + 5] = Chr ($b);
}
Functions for generating digital images
function Draw2digits ($x, $y, $number)
{
Draw_digit ($x, $y, (int) ($number/10));
Draw_digit ($x + one, $y, $number% 10);
}

function Draw_digit ($x, $y, $digit)
{
Global $SX, $sy, $pixels, $digits, $lines;

$digit = $digits [$digit];
$m = 8;
for ($b = 1, $i = 0; $i < 7; $i + +, $b *= 2) {
if ($b & $digit) = = $b) {
$j = $i * 4;
$x 0 = $lines [$j] * $m + $x;
$y 0 = $lines [$j + 1] * $m + $y;
$x 1 = $lines [$j + 2] * $m + $x;
$y 1 = $lines [$j + 3] * $m + $y;
if ($x 0 = $x 1) {
$ofs = 3 * ($SX * $y 0 + $x 0);
for ($h = $y 0; $h <= $y 1; $h + +, $ofs + + 3 * $sx) {
$pixels [$ofs] = chr (0);
$pixels [$ofs + 1] = chr (0);
$pixels [$ofs + 2] = chr (0);
}
} else {
$ofs = 3 * ($SX * $y 0 + $x 0);
for ($w = $x 0; $w <= $x 1; $w + +) {
$pixels [$ofs + +] = chr (0);
$pixels [$ofs + +] = chr (0);
$pixels [$ofs + +] = chr (0);
}
}
}
}
}

Add text to the image
function Add_chunk ($type)
{
Global $result, $data, $chunk, $CRC _table;

Chunk: For layers
Length:4 bytes: Used to compute chunk
Chunk Type:4 bytes
Chunk Data:length bytes
Crc:4 bytes: Cyclic Redundancy code checksum

Copy data and create CRC checksum
$len = strlen ($data);
$chunk = Pack ("c*", ($len >>) & 255,
($len >>) & 255,
($len >> 8) & 255,
$len & 255);
$chunk. = $type;
$chunk. = $data;

Calculate a CRC checksum with the bytes chunk[4..len-1]
$z = 16777215;
$z |= 255 << 24;
$c = $z;
for ($n = 4; $n < strlen ($chunk); $n + +) {
$c 8 = ($c >> 8) & 0xFFFFFF;
$c = $CRC _table[($c ^ ord ($chunk [$n])) & 0xFF] ^ $c 8;
}
$CRC = $c ^ $z;

$chunk. = Chr (($CRC >>) & 255);
$chunk. = Chr (($CRC >>) & 255);
$chunk. = Chr (($CRC >> 8) & 255);
$chunk. = Chr ($CRC & 255);

Add the results to the $result
$result. = $chunk;
}


Main program

$SX = 80;
$sy = 21;
$pixels = "";

Fill
for ($h = 0; $h < $sy; $h + +) {
for ($w = 0; $w < $sx; $w + +) {
$r = 100/$SX * $w + 155;
$g = 100/$sy * $h + 155;
$b = 255-($SX + $sy) * ($w + $h));
$pixels. = Chr ($r);
$pixels. = Chr ($g);
$pixels. = Chr ($b);
}
}

$date = getdate ();
$s = $date ["Seconds"];
$m = $date ["Minutes"];
$h = $date ["hours"];
$digits = Array (95, 5, 118, 117, 45, 121, 123, 69, 127, 125);
$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;

Draw2digits (4, 2, $h);
Draw2digits (2, $m);
Draw2digits (2, $s);
Set_4pixel (0, 0, 0, 26, 7);
Set_4pixel (0, 0, 0, 26, 13);
Set_4pixel (0, 0, 0, 52, 7);
Set_4pixel (0, 0, 0, 52, 13);

Create cyclic redundancy Code Check table
$z =-306674912; = 0xedb88320
for ($n = 0; $n < 256; $n + +) {
$c = $n;
for ($k = 0; $k < 8; $k + +) {
$c 2 = ($c >> 1) & 0x7fffffff;
if ($c & 1) $c = $z ^ ($c 2); else $c = $c 2;
}
$CRC _table[$n] = $c;
}

PNG File Signature
$result = Pack ("c*", 137,80,78,71,13,10,26,10);

IHDR Chunk Data:
Width:4 bytes
Height:4 bytes
Bit depth:1 byte (8 bits per RGB value)
Color Type:1 byte (2 = RGB)
Compression Method:1 byte (0 = deflate/inflate)
Filter Method:1 byte (0 = Adaptive filtering)
Interlace method:1 byte (0 = no interlace)
$data = Pack ("c*", ($sx >>) & 255,
($sx >>) & 255,
($SX >> 8) & 255,
$SX & 255,
($sy >>) & 255,
($sy >>) & 255,
($sy >> 8) & 255,
$sy & 255,
8,
2,
0,
0,
0);
Add_chunk ("IHDR");

The following do not dare to translate, please own reference
Scanline:
Filter byte:0 = None
RGB bytes for the line
The scanline is compressed with "zlib" and Method 8 (RFC-1950):
Compression method/flags code:1 byte ($78 = Method 8, 32k window)
Additional Flags/check bits:1 byte ($01:fcheck = 1, fdict = 0, flevel = 0)
Compressed data blocks:n bytes
One block (RFC-1951):
Bit 0:bfinal:1 for the last block
Bit 1 and 2:btype:0 for no compression
Next 2 Bytes:len (LSB i)
Next 2 Bytes:one ' s complement of LEN
LEN bytes Uncompressed Data
Check value:4 bytes (Adler-32 checksum of the uncompressed data)
//
$len = ($SX * 3 + 1) * $SY;
$data = Pack ("c*", 0x78, 0x01,
1,
$len & 255,
($len >> 8) & 255,
255-($len & 255),
255-(($len >> 8) & 255));
$start = strlen ($data);
$i 2 = 0;
for ($h = 0; $h < $sy; $h + +) {
$data. = chr (0);
for ($w = 0; $w < $SX * 3; $w + +) {
$data. = $pixels [$i 2++];
}
}


Calculate a Adler32 checksum with the bytes Data[start. LEN-1]
$s 1 = 1;
$s 2 = 0;
for ($n = $start; $n < strlen ($data); $n + +) {
$s 1 = ($s 1 + ord ($data [$n]))% 65521;
$s 2 = ($s 2 + $s 1)% 65521;
}
$adler = ($s 2 << 16) | $s 1;

$data. = Chr (($adler >>) & 255);
$data. = Chr (($adler >>) & 255);
$data. = Chr (($adler >> 8) & 255);
$data. = Chr ($adler & 255);
Add_chunk ("Idat");

Iend:marks the end of the Png-file
$data = "";
Add_chunk ("Iend");

Print image

Echo ($result);
?>

How to call, in fact very simple, save the above as timeimg.php
Then create a new page as follows:

<title>test</title>
<meta http-equiv= "Content-type" content= "text/html" >
<body>
//Call PHP file in image connection mode
</body>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.