Php plotting: how to write Chinese and English on images; _ PHP Tutorial

Source: Internet
Author: User
Tags image identifier
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...

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.