-
// 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"); // bbs.it-home.org
- 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.
-
// 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, mainly manifested in: 1. imagettftext () can output both Chinese and English, and can specify the font; imagestring () can only output English, you can only use the default font. 2. the imagettftext () font size can be infinitely large; imagestring () font size can only be 1 ~ 5. 3. the font output by imagettftext () can be changed to an angle. imagestring () can only be output horizontally. |