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

Source: Internet
Author: User

This program can generate PNG format images of the current time without using the GD library, which is of great reference to teaman.

<? 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.