Home  >  Article  >  Backend Development  >  PHP drawing skills how to write Chinese and English on pictures

PHP drawing skills how to write Chinese and English on pictures

WBOY
WBOYOriginal
2016-07-25 08:52:111110browse
  1. //1. Create canvas

  2. $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.
  3. $red = imagecolorallocate($im,255,0,0);

  4. //2. Writing

  5. $str = "hello,world";
  6. imagestring($im,5,30 ,60,$str,$red);//Parameter description: 5-refers to the size of the text. Function imagestring cannot write Chinese

  7. //3. Output image

  8. header("content-type: image/png"); //bbs.it-home.org
  9. imagepng($im) ;//Output to the page. If there is a second parameter [,$filename], it means saving the image

  10. //4. Destroy the image and release the memory

  11. imagedestroy($im);
  12. ?>
Copy code

Method 2, write in Chinese.

  1. //1. Create canvas

  2. $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.
  3. $red = imagecolorallocate($im,255,0,0);

  4. //2. Writing

  5. $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
  6. imagettftext($im,12,rand(0,20),20,100,$red,"simhei.ttf",$str);

  7. //3. Output image

  8. header ("content-type: image/png");
  9. imagepng($im);//Output to the page. If there is a second parameter [,$filename], it means saving the image

  10. //4. Destroy the image and release the memory

  11. imagedestroy($im);
  12. ?>
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.



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