PHP Method for outputting Chinese characters using the GD library [test available], phpgd

Source: Internet
Author: User

PHP Method for outputting Chinese characters using the GD library [test available], phpgd

This example describes how PHP outputs Chinese characters using the GD library. We will share this with you for your reference. The details are as follows:

I. Principles

The GD library in PHP supports Chinese, but must be passed in UTF-8 format parameters. Next let's talk about the UTF-8 encoding of Chinese characters.

1. Use notepad in WORD2000 or WINDOW 2000 to edit the Chinese characters to be displayed.

2. Save the edited text to a file in UTF-8 format.

3. open the file with a hexadecimal Editor (such as the EDIT function of PCTOOLS). Then, you can see that the opened file contains hexadecimal characters. A Chinese Character occupies three bytes, so you should select to edit the number of Chinese characters multiplied by three bytes, and record the encoding for future use. For example, the code for "helper House" is E8 84 9A E6 9C AC E4 B9 8B E5 AE B6

II. Implementation

In this example, the image output of the customer's House [url] www.jb51.net [/url] is implemented. The file name is ttf. php.

<? Php // define the output as the image type header ("content-type: image/gif"); // create an image $ pic = imagecreate (, 40 ); // define the black and white colors $ black = imagecolorallocate ($ pic, 255,255,255, 0); $ white = imagecolorallocate ($ pic,); // define the font $ font = "c: // WINDOWS // fonts // simhei. ttf "; // defines the output font string $ str = chr (0xE8 ). chr (0x84 ). chr (0x9A ). chr (0xE6 ). chr (0x9C ). chr (0xAC ). chr (0xE4 ). chr (0xB9 ). chr (0x8B ). chr (0xE5 ). chr (0xAE ). chr (0xB6 ). "[url] www.jb51.net [/url]"; // write TTF text to imagettftext ($ Pic, 20, 0, 10, 30, $ white, $ font, $ str); // create a GIF image imagegif ($ pic); // end the image, release the memory space imagedestroy ($ pic);?>

Output result

Note:

1. the reference method of Chinese characters is chr (encoding ). For example, chr (0xE8). chr (0x84). chr (0x9A) displays Chinese characters.

2. imageTTFText (int im, int size, int angle, int x, int y, int col, string fontfile, string text): This function writes TTF (TrueType Fonts) text to an image. The parameter size is the font size. angle is the angle of the font, which is calculated clockwise. 0 degrees is horizontal, that is, the direction of three o'clock (from left to right ), 90 degrees indicates the bottom-up text; x and y are the coordinate values of the text (the upper left corner of the origin); col is the color of the word; fontfile is the name of the fontfile; text is the string content.

3. You must install the GD library to use the image processing functions in php.

Image reference method: insert the image directly as needed:


You can.

Iii. Conclusion

The GD library of PHP provides quite a perfect function, making full use of its function to produce a variety of graphic images, so that the page content is richer.

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.