Tutorials to .com

Tutorials to .com » Php » Others » Described in the PHP-point method using "mapping" in Chinese

Described in the PHP-point method using "mapping" in Chinese

Print View , by: iSee ,Total views: 4 ,Word Count: 2372 ,Date: Sun, 19 Apr 2009 Time: 11:34 PM

Author: LuciferStar Source: Beyond php

Preface:
Now, more and more like the Internet, more and more people have their own personal home page. With the emergence of automated software tools to produce more and more simple page. However, available everywhere as a result of special effects, innovative things, fewer and fewer. Perhaps, the date, you will find a site on the same counters and their own. Increasingly sophisticated website. Things on the page, but also more and more, more and more rich.
Set Q:

On the page, if I have to add a counter:
Previously, providers would have to go to space links, or other places to copy the address, but some is always someone else to do, good or not, you do not have much of a say and can only find one by one .
On the page, I want to go out some information:
If the information is text, so a new page to add links;
Data, so a new page to add links:
If these data can be updated regularly, or even per hour, per minute, are likely to change, are you willing to observe in front of the computer and kept modify, upload it? (God is not a commercial site, no one wants the money-burning for you.)
And message boards, chat rooms, forums, these is not HTML and javascript alone will be able to get the.
In order to achieve more automation, you can use CGI (Common Gateway Interface) program to achieve these functions.

Software Requirements:
PHP: GD Library
To support the PHP server configuration. I OmniHTTPd Professional

Real-time data for the counters and statistics, publish, we can use to complete the picture. Output text in the picture.
In PHP, it is necessary to create a picture, and display the contents of the above, the basic steps are as follows:


<? php
/ / http header telling the browser that this is a GIF image
header ( "Content-type: image / gif");
/ / To paint, rather than depend upon the fabric? 400 × 300 to create a palette image
$ im = imagecreate (400, 300);
$ black = imagecolorallocate ($ im, 0, 0, 0);
/ / The default black background.
/ / (The default is the first definition refers to the color. If the definition of lines of code in front of another color, then, that the first definition is the default background color.)
$ red = imagecolorallocate ($ im, 255, 0, 0);
/ / Red. If the exchange of these two lines, you will find that the background is red, text is black.
$ string = "1234567890";
/ / To draw the characters
imagestring ($ im, 12,10,10, $ string, $ red);
/ / In the (10,10) to start drawing the string
imagepng ($ im);
/ / Output in png format, can also be used imagejpeg ($ im); or magegif ($ im); the latter, if GD is higher than version 1.6, will not be a.
imagedestroy ($ im);
/ / End, clear all the memory resources occupied
?>

Above example, 400 × 300 in the picture, since the point (10,10), the mapping of 12 pounds, "1234567890." Do you have noted that the size of this picture is: 251 bytes! You can also try the other output format.
Picture size, and background image pixel points in Central Africa, and has nothing to do with the output of the number of pixels.

However, there is a problem.
You can use imagestring () output the following information:
imagestring ($ im, 1,0,0, "abcdefghijklmnopqrstuvwxyz0123456789 ~!@#$%^&*()_+{}|:"<>?[]';,./",$ red);
However, you can not correct the output of Chinese! ! !
imagestring ($ im, 1,0,0, "ah", $ red);
You see, not the Chinese! ! But garbled.
PHP default character set is UTF-8, and Simplified Chinese is the GB2312.

How to solve? !
To solve this problem, you can extend the module to allow PHP to load php_iconv.dll (UNIT were under the suffix. SO), but, sometimes, may not work correctly. Originally, I would like to test code for someto this, how did not get success. In order to avoid errors, I still do notof them.
However, the most deadly, if you shut down the space service providers of the expansion modules, or even prohibit the load module DL () function, then, you only with the BYE-BYE Chinese.
Fortunately, there are other ways.
Character Map can be a good pre-code conversion table to output characters. However, you need a code table!
Alternatively, each hand-painted Chinese each point! How to feel? !

Well, come on, we draw together the word!

Word paintings, we must first know how to paint.
A simple function of middle school, learn, right? Function to draw the graphics, did you? Calculate the coordinates of a point, and then connect the two adjacent points. This method, called trace-point method.
We have to do is as much as possible to count them, and then displayed in the corresponding coordinates.
Do you have heard of dot-matrix printers, dot matrix characters?
Chinese characters in the output when they are used to express a point of.

Coordinates in a display a certain color is a function of the point:
int imagesetpixel (resource image, int x, int y, int color)
I have to assume that the coordinates (100100) display a white point, then only the following code:


<? php
header ( "Content-type: image / gif");
$ image = imagecreate (400, 300);
$ black = imagecolorallocate ($ image, 0, 0, 0);
$ white = imagecolorallocate ($ image, 255, 255, 255); / / definition of a white
imagesetpixel ($ image, 100, 100, $ white);
imagepng ($ image);
imagedestroy ($ image);
?>

In other words, we obtain a character as long as all the points of information, we will be able through this function, the output of that character.

Chs16.fon in the document, the save is GB area code table (the national standard of information exchange with the basic character set encoding of Chinese characters GB-2312). It is the Chinese character of the lattice. (WIN98 system, the file in c: windowscommand under. If you make it on the Unix system to use, please note that case. If not, you can find the link at the end.)
It is the MSDOS era, but, good things, or should one use out of.

From chs16.fon, we can read the Chinese characters dot-matrix data. Each character is from 16 × 16 points posed. Strokes taken place at a value of 1, otherwise 0; each point occupying a place, eight points each constitute a byte. In that case, a Chinese character would require (16 × 16 ÷ 8 = 32) bytes.

The following example is to illustrate that the method of dot matrix characters.
Here, the definition of a 8 × 8 matrix, showing the letter C, with 0 white box that said a black box with 1, then the eight lines of code graphics are:



Row
Said binary
Hexadecimal

0
00000000
0x00

1
00111110
0x3E

2,
01110000
0xE0

Three
01110000
0xE0

Four
01110000
0xE0

5,
01110000
0xE0

6,
00111110
0x3E

7
00000000
0x00




To the output of these points, it needs to draw the first line, and then the second line, third line ... ... to the last line.
With a circle:
for ($ hang = 0; $ hang <8; $ hang + +)
In each line, eight grid, needs to draw from the first, and then the second, third ... ... the last one.
With a circle:
for ($ gezi = 0; $ gezi <8; $ gezi + +)
Cycle together two columns:
for ($ hang = 0; $ hang <8; $ hang + +)
for ($ gezi = 0; $ gezi <8; $ gezi + +)
(/ / Here, we will be able to export point.
imagesetpixel ($ image, $ gezi, $ hang, $ color);
)

But, how do we know where to read Chinese characters, a dot-matrix information?

General character, such as ASCII code, is the number of 0 - 127 (ie binary 00000000-01111111) to show that, while the Chinese, it is high for 1 with two bytes (100000000 100000000) said. Such as: half-width character "A", machine code is (01000001) (it is actually ASCII code values).
Now, let us open the "Character Map" to see it. If you order to save disk, not installed, it loaded about, do not. If not installed, you see me nextit.
In the character mapping table, font choice, "italics _GB2312", click on the "special symbols," At this time, you can see the GB area code table, from the characters (10110000 10100001), the first has been to (10011111 11111111).
Full-width character "A", machine code is: (10100011 11000001) (it is actually two high ASCII code for 1).
Chinese "ah" of the machine code is (10110000 10100001);
In the GB-2312 character set, "ah" position in the table area 16, the first one, the coordinates (16,1), with binary that is (00010000,00000001). This is the "ah" of the area code.
Take a look:

Chinese characters: ah
Machine code: (10110000 10100001)
Area code: (00010000,00000001)
The difference between: (10100000,10100000)

Therefore,
Area code and machine code conversion formula】 【area code + (10100000 10100000) = 【Machine】 code. Namely:
Location Code 0 + (10100000) = machine code 0;
1 + area code (10100000) = machine code 1;
In that case, lattice data, we can through the Chinese characters "machine code" -> "area code" for the index to find.

Has been speaking in front of a Chinese character, in the table should occupy 32 bytes, so we contain the definition of an array of 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,0,0,0,0,
0,0);
Saved from the font used to read out 32 bytes of data.

The next question, a certain character, save the document in the end, what position do?

Used Chinese characters as a result of a 32 bytes, and GB-2312 Location Code Table 94 line 94, then, as long as know that the characters in the table is the first of several, and then multiplied by 32 on the list.
Therefore, the definition of offset:
$ offset = (94 * ($ qh-1) + ($ wh-1)) * 32;
$ qh said district (qu), $ wh said bit (wei); by 1, is because the PHP to start counting from 0.
Find, on the need to use fseek () function to the code table set in this location, and then read 32 bytes into $ buffer on the list.
In addition, because Chinese is composed by two bytes, while the front is given by 8 dot matrix example, a byte, so painting points to the code changed a bit:
for ($ hang = 0; $ hang <16; $ hang + +)
for ($ j = 0; $ j <2; $ j + +) / / because it is two bytes, so insert a cycle
for ($ gezi = 0; $ gezi <8; $ gezi + +)
(
imagesetpixel ($ image, $ gezi +8 * $ j, $ hang, $ color);
)

Well, we started programming it!


/*************************************
* File name: 'draw1.0.inc.php
** Chinese lattice output shows version 1.0
** Only provide simple operation: the default size of the output string to the picture of pure Chinese coordinates (0,0) on
** More features, please see the next version.
*
****************************************/
function draw ($ image, $ string, $ color)
(
$ fp = fopen ( "chs16.fon", "rb ");// reading binary matrix font chs16.fon
if (! feof ($ fp)) / / if the file pointer at the end of the file, exit, do not forget to close a document
(
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, in fact, is to obtain a Chinese machine code.
substr ($ string, 0,1); is $ string obtained from the first byte, and then, through the ord (); this character is converted to an integer. (Because PHP does not support unsigned integers, there is no conversion of this step, you can only get a 0.) After an integer conversion can be calculated. Less machine code 0xa0 (10100000), on the received location code.
substr ($ string, 1,2); is to obtain $ string in the second byte. * /
$ offset = (94 * ($ qh-1) + ($ wh-1)) * 32;
/ * Get the character of the area value, calculated on the offset. * /
fseek ($ fp, $ offset, SEEK_SET);
/ * $ Fp in the font file, the file position pointer to offset. * /
$ buffer = preg_split ('//', fread ($ fp, 32), -1, PREG_SPLIT_NO_EMPTY);
/ * Fread ($ fp, 32); is $ fp read 32 bytes of data, and then through the preg_split (); assigned to the array in the $ buffer. preg_split (); is a support function are expressed. On regular expressions, I was learning. Why do, and I do not know. PHP manual, there are the examples. * /
for ($ i = 0; $ i <16; $ i + +) / / dot-matrix number of rows: 16 rows should be 16
for ($ j = 0; $ j <2; $ j + +) / / because it is two bytes, then one by one will be drawn
for ($ k = 0; $ k <8; $ k + +) / / each byte has 8 points of data
if (((ord ($ buffer [$ i * 2 + $ j ])>>( 7 - $ k)) & 0x01)) / / If the point value of 1, output; otherwise, no
(
imagesetpixel ($ image, $ x +8 * $ j + $ k, $ i, $ color);
)
$ string = substr ($ string, 2); / / Chinese expressed by two bytes, so the output after a character, it is necessary to remove the two bytes.
$ x = 24; / / output the end of a Chinese character, open space that the characters to the next. Because the character is 16 × 16 points, then, $ x's value is set to 16, enough. However, not too crowded?
)
)
fclose ($ fp);
)
Now, I give a test case:


<? php
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);
?>

For this function, we can also expand in order to achieve different results.



php other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.