PHP uses the GD library to output Chinese characters "test available" _php tips

Source: Internet
Author: User
Tags chr int size

This article is an example of how PHP uses the GD library to export Chinese characters. Share to everyone for your reference, specific as follows:

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 into a file formatted as UTF-8 format.

3. Open the file with a 16-in 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. The code for the cloud-dwelling community is: E8 9A E6 9C AC E4 B9 8B E5 AE B6

Second, realize

In this example, the image output of the cloud-dwelling community [Url]www.jb51.net[/url] is implemented. The file name is: ttf.php.

<?php
//Defines output as Image type
header ("Content-type:image/gif");
New Image
$pic =imagecreate (500,40);
Defines black and white color
$black =imagecolorallocate ($pic, 0,0,0);
$white =imagecolorallocate ($pic, 255,255,255);
Define 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 the figure
Imagettftext ($pic, 20,0,10,30, $white, $font, $str);
Establishment of GIF
imagegif ($pic);
End graphics, freeing memory space
Imagedestroy ($pic);
? >

Output as shown in figure:

Description

1. Chinese characters are referred to CHR (encoded). such as 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 will TTF (TrueType Fonts ) font text is written 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 function in PHP.

Image Reference mode: Insert directly where needed:

 
 

Can.

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.

More about PHP Interested readers can view the site topics: "PHP GD library Operation tips Summary", "PHP graphics and pictures Operating skills summary", "PHP basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Programming Program", " PHP Network Programming skills Summary, "PHP array Operation techniques Encyclopedia", "PHP string (String) Usage Summary", "Php+mysql database operations to get Started" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.