Php plotting: Writing Chinese and English on images ,. Php plotting: how to write Chinese and English on images. This document describes how to write Chinese and English on images in php. Share it with you for your reference. The following describes how to write Chinese and English on an image in php,
This example describes how to write Chinese and English on an image in php. Share it with you for your reference. The details are as follows:
The first method is to write only English, and garbled characters may appear in Chinese.
The code is as follows:
<? Php
// 1. create a canvas
$ Im = imagecreatetruecolor (300,200); // create a real-color image. the default background is black and the image identifier is returned. Another function, imagecreate, is not recommended.
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
// 2. write
$ Str = "hello, world ";
Imagestring ($ im, 5, 30, 60, $ str, $ red); // parameter description: 5-indicates the size of the text. Function imagestring cannot write Chinese characters
// 3. output image
Header ("content-type: image/png ");
Imagepng ($ im); // output to the page. If the second parameter [, $ filename] exists, the image is saved.
// 4. destroy images and release memory
Imagedestroy ($ im );
?>
Method 2: Write Chinese
The code is as follows:
<? Php
// 1. create a canvas
$ Im = imagecreatetruecolor (300,200); // create a real-color image. the default background is black and the image identifier is returned. Another function, imagecreate, is not recommended.
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
// 2. write
$ Str = iconv ("gb2312", "UTF-8", "Beijing, you are early! Hello, world "); // The file format is gbk, and here to the uft-8 format, in order to normal output, otherwise it is garbled. Unknown
Imagettftext ($ im, 12, rand (20,100), $ red, "simhei. ttf", $ str );
// 3. output image
Header ("content-type: image/png ");
Imagepng ($ im); // output to the page. If the second parameter [, $ filename] exists, the image is saved.
// 4. destroy images and release memory
Imagedestroy ($ im );
?>
The imagettftext () function is far better than the imagestring () function, which is manifested in the following aspects:
(1) imagettftext () can output both Chinese and English characters. you can specify the font. imagestring () can only output English characters and can only use the default font.
(2) the size of the imagettftext () font can be infinitely large; imagestring () font can only be 1 ~ 5.
(3) the font output by imagettftext () can be changed to an angle. imagestring () can only be output horizontally.
I hope this article will help you with php programming.
Examples: This article describes how to write Chinese and English on an image in php. Share it with you for your reference. The details are as follows...