Heim  >  Artikel  >  Backend-Entwicklung  >  php绘图技巧如何在图片上写中文与英文

php绘图技巧如何在图片上写中文与英文

WBOY
WBOYOriginal
2016-07-25 08:52:111069Durchsuche
  1. //1、创建画布

  2. $im = imagecreatetruecolor(300,200);//新建一个真彩色图像,默认背景是黑色,返回图像标识符。另外还有一个函数 imagecreate 已经不推荐使用。
  3. $red = imagecolorallocate($im,255,0,0);
  4. //2、写字

  5. $str = "hello,world";
  6. imagestring($im,5,30,60,$str,$red);//参数说明:5-指文字的大小。函数 imagestring 不能写中文
  7. //3、输出图像

  8. header("content-type: image/png"); //bbs.it-home.org
  9. imagepng($im);//输出到页面。如果有第二个参数[,$filename],则表示保存图像
  10. //4、销毁图像,释放内存

  11. imagedestroy($im);
  12. ?>
复制代码

方法2,写中文。

  1. //1、创建画布

  2. $im = imagecreatetruecolor(300,200);//新建一个真彩色图像,默认背景是黑色,返回图像标识符。另外还有一个函数 imagecreate 已经不推荐使用。
  3. $red = imagecolorallocate($im,255,0,0);
  4. //2、写字

  5. $str = iconv("gb2312","utf-8","北京,你早!hello,world");//文件格式为gbk,而这里转为uft-8格式,才能正常输出,否则也为乱码。表示不明
  6. imagettftext($im,12,rand(0,20),20,100,$red,"simhei.ttf",$str);
  7. //3、输出图像

  8. header("content-type: image/png");
  9. imagepng($im);//输出到页面。如果有第二个参数[,$filename],则表示保存图像
  10. //4、销毁图像,释放内存

  11. imagedestroy($im);
  12. ?>
复制代码

imagettftext() 函数远强于imagestring() 函数,主要表现: 1、imagettftext() 可以输出中文和英文,可以指定字体;imagestring() 只能输出英文,只能使用默认字体。 2、imagettftext() 字体大小可以无限大;imagestring() 字体只有1~5号大小。 3、imagettftext() 输出的字体可以变换角度;imagestring() 只能水平输出。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn