"Draw" Chinese characters using the dot method in PHP

Source: Internet
Author: User
Nowadays, more and more people prefer to access the Internet, and more people have their own personal homepages. With the emergence of various automated software tools, making web pages easier and easier. However, because special effects are available everywhere, there are fewer and fewer innovative things. Maybe, someday, you will find that the counter on a website is exactly the same as your own. The more sophisticated the web page is. Preface on the webpage:
Nowadays, more and more people prefer to access the Internet, and more people have their own personal homepages. With the emergence of various automated software tools, making web pages easier and easier. However, because special effects are available everywhere, there are fewer and fewer innovative things. Maybe, someday, you will find that the counter on a website is exactly the same as your own. The more sophisticated the web page is. There are more and more things on the web page.
Question:

On the webpage, if I want to add a counter:
In the past, I went to the space provider to get a link or copy an address from other places. However, this is always done by someone else. well, you don't have much say, you can only find them one by one.
On the webpage, I want to publish some information:
If the information is text, create a new page and add a link;
Is data. create a new page and add a link:
But if the data is updated frequently, or even every hour or minute, it may change. are you willing to keep changing and uploading data in front of your computer? (We are not a commercial website. no one is willing to burn money for you .)
However, message boards, chat rooms, and forums cannot be handled by HTML or javascript alone.
To achieve more automatic control, you can use the CGI (Common Gateway Interface) program to implement these functions.

Software Requirements:
PHP: GD Library
Configure a server that supports PHP. I use OmniHTTPd Professional

For counters and real-time statistics and releases, we can use images to complete them. Output text in the image.
In PHP, to create an image and display the dot content on it, follow these steps:


// The http header, telling the browser that this is a GIF image
Header ("Content-type: image/gif ");
// Do you want to draw a picture? Create a 400x300 color palette image
$ Im = imagecreate (400,300 );
$ Black = imagecolorallocate ($ im, 0, 0, 0 );
// The default black background.
// (The default value is the first defined color. If another color is defined before this line of code, the first color defined is the default background color .)
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
// Red. If the two lines are exchanged, you will find that the background is red and the text is black.
$ String = "1234567890 ";
// The characters to be drawn
Imagestring ($ im, 12, 10, 10, $ string, $ red );
// Draw a string at (10, 10)
Imagepng ($ im );
// Output in png format. you can also use imagejpeg ($ im); or magegif ($ im). However, if the latter version is higher than 1.6, it cannot be used.
Imagedestroy ($ im );
// End, clear all occupied memory resources
?>

In the preceding example, a 12-pound "400" image is drawn from a point (10, 10 ". Have you noticed that the image size is 251 bytes! You can also try other output formats.
The image size depends on the number of non-background pixels in the image and the number of output pixels.

However, there is a problem.
You can use imagestring () to output the following information:
Imagestring ($ im, 1, 0, 0, "abcdefghijklmnopqrstuvwxyz0123456789 ~! @ # $ % ^ & * () _ + {} |: "<>? [] ';,./", $ Red );
However, you cannot output Chinese characters correctly !!!
Imagestring ($ im, 1, 0, 0, "ah", $ red );
What you see is by no means Chinese !! But garbled characters.
PHP's default character set is UTF-8, while Simplified Chinese is GB2312.

How can this problem be solved ?!
To solve this problem, you can let PHP load the extension module php_iconv.dll (the extension name under UNIT is. SO). However, sometimes it may not work properly. Originally, I wanted to put a piece of test code up, but this time I was not successful. To avoid errors, I still don't put them on.
However, the most critical thing is that if your space service provider disables the extension module or even disables the DL () function of the module, you can only follow the Chinese BYE-BYE.
Fortunately, there are other methods.
Character ING can be used to output characters in a pre-converted code table. However, you need a code table!
Or, draw every Chinese point manually! How is it ?!

Okay, come on, let's draw words together!

To draw a word, you must first know how to draw it.
Have you learned simple functions in junior high school? Do you want to draw a graph of a function? Calculate the coordinates of a point and connect the adjacent points. This method is called the plotting method.
What we need to do is calculate as many points as possible and then display them in the corresponding coordinates.
Have you ever heard of dot matrix printers and dot matrix Chinese characters?
When outputting Chinese characters, they are represented by vertices.

The function for displaying a color point on a coordinate is:
Int imagesetpixel (resource image, int x, int y, int color)
If I want to display a white point at coordinate (100,100), the following code is required:


Header ("Content-type: image/gif ");
$ Image = imagecreate (400,300 );
$ Black = imagecolorallocate ($ image, 0, 0, 0 );
$ White = imagecolorallocate ($ image, 255,255,255); // defines white
Imagesetpixel ($ image, 100,100, $ white );
Imagepng ($ image );
Imagedestroy ($ image );
?>

That is to say, we can use this function to output all vertices of a Chinese character.

In the chs16.fon file, it is saved in the national standard location code table (GB-2312 of basic character set of Chinese character encoding used for national standard information exchange ). It is a dot matrix font for Chinese characters. (In WIN98, this file is in c: windowscommand. If you want to use it in a UNIX system, pay attention to the case. If not, you can find the link at the end of the article .)
It is in the MSDOS era, but good things should still be used out.

From chs16.fon, we can read the dot matrix data of Chinese characters. Each Chinese character consists of 16x16 points. Where the stroke goes, the vertex value is 1; otherwise, it is 0. each vertex occupies one position and every eight points constitute a byte. Therefore, a Chinese character requires (16 × 16 & pide; 8 = 32) bytes.

The following example is used to describe the representation of character lattice.
Here, an 8x8 matrix is defined, and a letter C is displayed. the white square is represented by 0, and the black square is represented by 1, the code for these eight lines of images is:



Line
Binary representation
Hexadecimal representation

0
00000000
0x00

1
00111110
0x3E

2
01110000
0xE0

3
01110000
0xE0

4
01110000
0xE0

5
01110000
0xE0

6
00111110
0x3E

7
00000000
0x00




To output these points, you need to first draw the first line, then the second line, the third line ...... To the last line.
Use a loop:
For ($ hang = 0; $ hang <8; $ hang ++)
There are eight grids in each row, which need to be drawn separately, from the first, then the second, and the third ...... To the last one.
Use a loop:
For ($ gezi = 0; $ gezi <8; $ gezi ++)
Two repeating columns:
For ($ hang = 0; $ hang <8; $ hang ++)
For ($ gezi = 0; $ gezi <8; $ gezi ++)
{// Here, we can output the vertex.
Imagesetpixel ($ image, $ gezi, $ hang, $ color );
}

But how do we know where to read the dot matrix data of a Chinese character?

General characters, such as ASCII code, are represented by numbers 0--127 (that is, binary 00000000 to 01111111), while Chinese characters are represented by two bytes (100000000 100000000) with a high value of 1. For example, the inner code of the half-width character "A" is (01000001) (it is actually an ASCII code value ).
Next, let's open the character ing table. If you haven't installed it to save disk space, just install it. If it is not installed, you can take a look at me.
In the character ing table, select "_ GB2312" as the font and click "special characters". then, you can see the national standard location code table, starting from the character (10110000 10100001, until (10011111 11111111 ).
The full-angle character "A", with the inner code: (10100011 11000001) (it is actually two ASCII codes with A high position of 1 ).
The inner code of the Chinese "ah" is (10110000 10100001 );
In the GB-2312 character set, "Ah" is in the table at the location of the 16th zone 1st bits, the coordinate (16, 1), expressed in binary, is (000213,00000001 ). This is the location code of "ah.
See:

Chinese characters: Ah
Internal Code: (10110000 10100001)
Location Code: (000213,00000001)
Difference: (10100000,10100000)

So,
The conversion formula of the location code and the inner code is [location code] + (10100000 10100000) = [inner code ]. That is:
Location Code 0 + (10100000) = internal code 0;
Location Code 1 + (10100000) = internal code 1;
In this way, the dot matrix data can be indexed and searched through the Chinese character "in-memory code"-> "location code.

We have mentioned a Chinese character which occupies 32 bytes in the table. Therefore, we define an array containing 32 elements:
$ Buffer = array (0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0 );
It is used to store 32 bytes of data read from the font.

The next question is, where is a character stored in the file?

Since a Chinese character uses 32 bytes, and the GB-2312 location code table has 94 rows, 94 columns, as long as you know the character in the table is the first few, multiply by 32 on the line.
So define the offset:
$ Offset = (94 * ($ qh-1) + ($ WH-1) * 32;
$ Qh indicates the partition (qu) and $ wh indicates the bit (wei); minus 1 because PHP starts counting from 0.
To locate the position, you only need to use the fseek () function to set the position of the code table, and then read 32 bytes to $ buffer.
In addition, because the Chinese character is composed of two bytes, and the dot matrix example given above is 8 bits, one byte, you need to modify the vertices code:
For ($ hang = 0; $ hang <16; $ hang ++)
For ($ j = 0; $ j <2; $ j ++) // Insert a loop because it is two bytes.
For ($ gezi = 0; $ gezi <8; $ gezi ++)
{
Imagesetpixel ($ image, $ gezi + 8 * $ j, $ hang, $ color );
}

Okay. let's start programming!


/*************************************
* File name: 'draw1. 0. inc. php
** Chinese Dot Matrix Output version 1.0
** Only simple operations are provided: output a pure Chinese character string of the default size to the coordinate (0, 0) of the image.
** For more functions, see the next version.
*
****************************************/
Function draw ($ image, $ string, $ color)
{
$ Fp = fopen ("chs16.fon", "rb"); // read the dot matrix font chs16.fon in binary mode
If (! Feof ($ fp) // if the file pointer reaches the end of the file, exit and do not forget to close the file.
{
While ($ string) // when the string is not 0
{
$ Qh = ord (substr ($ string, 0, 1)-0xa0;
$ Wh = ord (substr ($ string, 1, 2)-0xa0;
/* These two lines of code actually get a Chinese internal code.
Substr ($ string,); is to get the first byte from $ string, and then convert this character to an integer through ord. (Because PHP does not support unsigned integers, you can only get 0 if this step is not converted .) After being converted to an integer, the computation can be performed. The inner code minus 0xa0 (10100000) gets the location code.
Substr ($ string, 1, 2); is the second byte in $ string. */
$ Offset = (94 * ($ qh-1) + ($ WH-1) * 32;
/* After the location value of the Chinese character is obtained, the offset is calculated. */
Fseek ($ fp, $ offset, SEEK_SET );
/* In the font file $ fp, locate the file pointer to the offset. */
$ Buffer = preg_split ('//', fread ($ fp, 32),-1, PREG_SPLIT_NO_EMPTY );
/* Fread ($ fp, 32); reads 32 bytes of data from $ fp, and then distributes the data to the array $ buffer through preg_split. Preg_split (); is a function that supports regular expression. I am learning about regular expressions. I don't know why. This example is provided in the PHP Manual. */
For ($ I = 0; $ I <16; $ I ++) // number of dot matrix rows: 16 columns should also be 16
For ($ j = 0; $ j <2; $ j ++) // because it is two bytes, you need to draw them one by one.
For ($ k = 0; $ k <8; $ k ++) // Each byte has 8 points of data
If (ord ($ buffer [$ I * 2 + $ j])> (7-$ k) & 0x01 )) // if the value of this vertex is 1, output; otherwise, no
{
Imagesetpixel ($ image, $ x + 8 * $ j + $ k, $ I, $ color );
}
$ String = substr ($ string, 2); // Chinese is represented by two bytes. Therefore, after outputting a Chinese character, remove two bytes.
$ X = 24; // The output of a Chinese character ends. leave it blank and give the next Chinese character. Because the Chinese character is 16x16, it is enough to set the value of $ x to 16. But it's too crowded, isn't it?
}
}
Fclose ($ fp );
}
Below is a test example:


Header ("Content-type: image/gif ");
Include 'draw1. 0. inc. php ';
$ Im = imagecreate (400,300 );
$ Black = imagecolorallocate ($ im, 0, 0, 0 );
$ String = "Chinese ";
Drawer ($ im, $ string );
Imagepng ($ im );
Imagedestroy ($ im );
?>

We can also expand this function to achieve different effects.

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.