Home  >  Article  >  Backend Development  >  PHP drawing method to write Chinese and English on pictures, _PHP tutorial

PHP drawing method to write Chinese and English on pictures, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:08832browse

How to write Chinese and English on pictures with php drawing,

The example in this article describes the method of writing Chinese and English on pictures in PHP drawing. Share it with everyone for your reference. The details are as follows:

The first method can only be written in English, and Chinese characters will be garbled

Copy code The code is as follows:
//1. Create canvas
$im = imagecreatetruecolor(300,200);//Create a new true color image, the default background is black, and return the image identifier. There is also a function imagecreate that has been deprecated.
$red = imagecolorallocate($im,255,0,0);
//2. Writing
$str = "hello,world";
imagestring($im,5,30,60,$str,$red);//Parameter description: 5-refers to the size of the text. Function imagestring cannot be written in Chinese
//3. Output image
header("content-type: image/png");
imagepng($im);//Output to the page. If there is a second parameter [,$filename], it means saving the image
//4. Destroy the image and release memory
imagedestroy($im);
?>

The second method: write in Chinese

Copy code The code is as follows:
//1. Create canvas
$im = imagecreatetruecolor(300,200);//Create a new true color image, the default background is black, and return the image identifier. There is also a function imagecreate that has been deprecated.
$red = imagecolorallocate($im,255,0,0);
//2. Writing
$str = iconv("gb2312","utf-8","Good morning, Beijing! hello, world");//The file format is gbk, and it is converted to uft-8 format here to output normally, otherwise it will be Garbled characters. Unknown
imagettftext($im,12,rand(0,20),20,100,$red,"simhei.ttf",$str);
//3. Output image
header("content-type: image/png");
imagepng($im);//Output to the page. If there is a second parameter [,$filename], it means saving the image
//4. Destroy the image and release memory
imagedestroy($im);
?>

The imagettftext() function is much stronger than the imagestring() function, which is reflected in the following aspects:

(1) imagettftext() can output Chinese and English, and the font can be specified; imagestring() can only output English, and can only use the default font.
(2) The font size of imagettftext() can be infinite; the font size of imagestring() is only size 1~5.
(3) The font output by imagettftext() can change the angle; imagestring() can only output horizontally.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947208.htmlTechArticleHow to write Chinese and English on pictures with PHP drawing. This article explains how to write Chinese and English on pictures with PHP drawing. Methods in Chinese and English. Share it with everyone for your reference. The details are as follows: Chapter...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn