"Draw" Chinese characters using the dot method in PHP

Source: Internet
Author: User

Author: LuciferStar Source: Beyond PHP preface: 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. Q: On the webpage, if I want to add a counter: Previously, I would like to go to the Space Provider to get a link, or copy an address elsewhere. However, it is always done by others. Good or bad, you do not have much say, you can only find 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; if it 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 configuration supports PHP servers. I use OmniHTTPd Professional for counters and real-time statistics and publishing, we can use images to complete. Output text in the image. In PHP, to create an image and display the dot content on it, follow these steps: 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 correctly !!! Imagestring ($ im, 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 point of a certain color on a coordinate is: int imagesetpixel (resource image, int x, int y, int color). Assume that I want to be in the coordinate (100,100) the following code shows a white point: 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 (16x16 bytes 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: the binary value of the row indicates that the hexadecimal value is 0 00000000 0x00 1 00111110 0x3E 2 01110000 0xE0 3 01110000 0xE0 4 01110000 0xE0 5 01110000 0xE0 6 00111110 0x3E 7 00000000 0x00., 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 ++) in each row, there are eight grids, which need to be drawn separately, from the first, then the second and third ...... To the last one. Use a loop: for ($ gezi = 0; $ gezi <8; $ gezi ++) Two cyclic join 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 ). English "ah" inner code, is (10110000 10100001); In the GB-2312 character set, "Ah" in the table location is 16th zone 1st bits, this coordinate (16, 1 ), it is represented in binary format (bytes, bytes ). This is the location code of "ah. Please refer to: Chinese Character: Ah machine internal code: (10110000 10100001) Location Code: (,) Difference: (,) So, the conversion formula of the location code and the inner code is [Location Code] + (10100000 10100000) = [inner Code ]. That is, the location code 0 + (10100000) = the inner code 0; the location code 1 + (10100000) = the inner code 1; in this case, the dot matrix data, you can use the Chinese character "in-host code"-> "location code" for indexing and searching. We have already mentioned a Chinese character which occupies 32 bytes in the table. Therefore, we have defined 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 ); 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 offset: $ offset = (94 * ($ qh-1) + ($ wh-1) * 32; $ qh represents the partition (qu), $ wh represents 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 ++) // because it is two bytes, therefore, insert a loop for ($ gezi = 0; $ gezi <8; $ gezi ++) {imagesetpixel ($ image, $ gezi + 8 * $ j, $ hang, $ color) ;}good. Let's start programming! /************************************ File name: draw1.0.inc. php ** Chinese display lattice output version 1.0 ** only provides simple operations: outputs a default Chinese character string of the Chinese size to the coordinate (0, 0) of the image ** more functions, see the next version. **************************************** */Function draw ($ image, $ string, $ color) {$ fp = fopen ("chs16.fon", "rb"); // read the dot matrix font chs16.fon 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);/* locate the file pointer to the offset in the font $ fp file. */$ Buffer = preg_split (//, fread ($ fp, 32),-1, PREG_SPLIT_NO_EMPTY );/

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.