·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> php 加载字体 并保存成图片

php 加载字体 并保存成图片

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
php 加载字体 并保存成图片[php]view plaincopy在CODE上查看代码片派生到我的代码片
  1. //Setthecontent-type
  2. header("Content-type:image/png");
  3. //Createtheimage
  4. $im=imagecreatetruecolor(400,100);
  5. //Createsomecolors
  6. $white=imagecolorallocate($im,255,255,255);
  7. $grey=imagecolorallocate($im,128,128,128);
  8. $black=imagecolorallocate($im,0,0,0);
  9. imagefilledrectangle($im,0,0,399,100,$white);
  10. //Thetexttodraw
  11. $text='字典网';
  12. //Replacepathbyyourownfontpath
  13. $font='fontName.ttf';
  14. //Addsomeshadowtothetext
  15. //imagettftext($im,60,0,11,21,$grey,$font,$text);
  16. //Addthetext
  17. imagettftext($im,60,0,0,70,$black,$font,$text);
  18. //Usingimagepng()resultsinclearertextcomparedwithimagejpeg()
  19. imagepng($im);
  20. imagedestroy($im);

如果想保存图可以用下面代码

[html]view plaincopy在CODE上查看代码片派生到我的代码片
    1. ob_start();
    2. imagejpeg($im);
    3. $img=ob_get_contents();
    4. ob_end_clean();
    5. $size=strlen($img);
    6. $fp2=@fopen('tst.jpg',"a");
    7. fwrite($fp2,$img);
    8. fclose($fp2);