Using GD to output Chinese characters in PHP

Source: Internet
Author: User
Tags define chr int size reference
Chinese characters (Wang Feng)


First, the principle
The GD Library in PHP supports Chinese, but must be passed in UTF-8 format parameters. First of all, the UTF-8 encoding of Chinese characters is obtained.
    1. Edit the characters you want to display with Notepad in WORD2000 or Window 2000.
    2. Save the edited text in a file formatted as UTF-8.
    3. Open the file with a 16-binary editor (such as the PCTools Edit feature), and you will see that there are 16 characters in the open file. A Chinese character occupies three bytes, so you should choose to edit the number of Chinese characters multiplied by 3 bytes of content, the encoding is recorded for later use. such as the code of the Sadie Network is:E8 B5 9B E8 BF AA E7 BD

Second, realize
This example realizes: The www.ccidnet.com image output of the Sadie net. The file name is: ttf.php. <?php
Define output as Image type
Header ("Content-type:image/gif");
New Image
$pic =imagecreate (240,30);
Define black and white colors
$black =imagecolorallocate ($pic, 0,0,0);
$white =imagecolorallocate ($pic, 255,255,255);
Defining fonts
$font = "C://win2000//fonts//simhei.ttf";
Define output Font string
$STR = Chr (0xe8). chr (0xb5). chr (0x9b). chr (0xe8). chr (0xBF). chr (0xAA). chr (0xe7). chr (0xBD). chr (0x91). Www.ccidnet.com ";
Write TTF text into the diagram
Imagettftext ($pic, 20,0,10,20, $white, $font, $STR);
Create a GIF pattern
Imagegif ($pic);
End graphics, freeing up memory space
Imagedestroy ($pic);
?>

Output as shown in figure:

Description
    1. The Chinese character is CHR (encoded) by the way of reference. such as Chr (0xe8). chr (0xb5). chr (0x9b) displays Chinese characters.
    2. Imagettftext (int im, int size, int angle, int x, int y, int col, string fontfile, String text): This function will TTF (TrueType Fonts) word Type text to the picture. The size of the parameter is the dimension of the glyph; angle is the angle of the font. Clockwise calculation, 0 degree level, that is, the direction of three o'clock (from left to right), 90 degrees from the bottom of the text; x,y two parameter is the coordinate value of the text (the origin is the upper-left corner); Col is the color of the word; Fontfile is the font The file name; text is the string content.
    3. The GD library must be installed to take advantage of the image processing functions in PHP.
    4. Image reference: Insert directly in the desired place: .

Third, concluding remarks
The GD Library of PHP provides a fairly sophisticated feature that makes it possible to produce a wide variety of graphic images with the full use of its functionality, which enriches the content of the page.

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.