Programs that generate PNG images of the current time without using the GD library

Source: Internet
Author: User

TheProgramIt is helpful for teaman to generate PNG images of the current time without using the GD library.

<? 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 );
}
// Function for generating digital images
Function draw2digits ($ X, $ y, $ number)
{
Draw_digit ($ X, $ y, (INT) ($ number/10 ));
Draw_digit ($ x + 11, $ 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;
$ X0 = $ lines [$ J] * $ m + $ X;
$ Y0 = $ lines [$ J + 1] * $ m + $ Y;
$ X1 = $ lines [$ J + 2] * $ m + $ X;
$ Y1 = $ lines [$ J + 3] * $ m + $ Y;
If ($ X0 = $ X1 ){
$ OFS = 3 * ($ SX * $ y0 + $ X0 );
For ($ H = $ y0; $ H <= $ Y1; $ H ++, $ OFS + = 3 * $ SX ){
$ Pixels [$ OFS] = CHR (0 );
$ Pixels [$ OFS + 1] = CHR (0 );
$ Pixels [$ OFS + 2] = CHR (0 );
}
} Else {
$ OFS = 3 * ($ SX * $ y0 + $ X0 );
For ($ W = $ x0; $ W <= $ x1; $ 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: Layer
// Length: 4 Bytes: used to calculate chunk
// Chunk type: 4 bytes
// Chunk data: length bytes
// CRC: 4 Bytes: cyclic Verification

// Copy data and create CRC checksum
$ Len = strlen ($ data );
$ Chunk = pack ("C *", ($ Len> 24) & 255,
($ Len> 16) & 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 ++ ){
$ C8 = ($ C> 8) & 0 xffffff;
$ C = $ crc_table [($ C ^ ord ($ chunk] [$ N]) & 0xff] ^ $ C8;
}
$ CRC = $ C ^ $ Z;

$ Chunk. = CHR ($ CRC> 24) & 255 );
$ Chunk. = CHR ($ CRC> 16) & 255 );
$ Chunk. = CHR ($ CRC> 8) & 255 );
$ Chunk. = CHR ($ CRC & 255 );

// Add the result to $ 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-(100/($ 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 (30, 2, $ m );
Draw2digits (56, 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 a cyclic verification table
$ Z =-306674912; // = 0xedb88320
For ($ n = 0; $ n <256; $ n ++ ){
$ C = $ N;
For ($ k = 0; $ k <8; $ K ++ ){
$ C2 = ($ C> 1) & 0x7fffffff;
If ($ C & 1) $ c = $ Z ^ ($ C2); else $ c = $ C2;
}
$ Crc_table [$ N] = $ C;
}

// PNG file Signature
$ Result = pack ("C );

// 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> 24) & 255,
($ SX> 16) & 255,
($ SX> 8) & 255,
$ SX & 255,
($ Sy> 24) & 255,
($ Sy> 16) & 255,
($ Sy> 8) & 255,
$ Sy & 255,
8,
2,
0,
0,
0 );
Add_chunk ("ihdr ");

// Do not translate the text below. Please refer to it yourself
// Scanline:
// Filter byte: 0 = none
// RGB bytes for the line
// The scanline is compressed with "zlib", Method 8 (RFC-1950 ):
// Compression method/flags code: 1 byte ($78 = Method 8, 32 K 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 first)
// 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 );
$ I2 = 0;
For ($ h = 0; $ H <$ sy; $ H ++ ){
$ Data. = CHR (0 );
For ($ W = 0; $ W <$ SX * 3; $ W ++ ){
$ Data. = $ pixels [$ I2 ++];
}
}

// Calculate a adler32 checksum with the bytes data [start .. len-1]
$ S1 = 1;
$ S2 = 0;
For ($ n = $ start; $ n <strlen ($ data); $ n ++ ){
$ S1 = ($ S1 + ord ($ data [$ N]) % 65521;
$ S2 = ($ S2 + $ S1.) % 65521;
}
$ Adler = ($ S2 <16) | $ S1;

$ Data. = CHR ($ Adler> 24) & 255 );
$ Data. = CHR ($ Adler> 16) & 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 the image

Echo ($ result );
?>

// How to call it is actually very simple. Save the above as timeimg. php
// Create a new page as follows:

<HTML>
<Head>
<Title> test </title>
<Meta http-equiv = "Content-Type" content = "text/html">
</Head>
<Body>
// call the PHP file through image connection
</Body>
</Html>

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.