-
-
//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 write Chinese
//3. Output image
- header("content-type: image/png"); //bbs.it-home.org
- imagepng($im) ;//Output to the page. If there is a second parameter [,$filename], it means saving the image
//4. Destroy the image and release the memory
- imagedestroy($im);
- ?>
-
Copy code
Method 2, write in Chinese.
-
-
//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","Beijing, Good morning! Hello, world");//The file format is gbk, and it is converted to uft-8 format to output normally, otherwise it will be garbled. Unknown expression
- 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 the memory
- imagedestroy($im);
- ?>
-
Copy code
imagettftext() function is much stronger than imagestring() function, its main performance is:
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.
|