php 图片写字

图片写文字

  • 在已经创建为文件的图片上写字

     function inTextImg($path){
         //复制一个新的文件
         $im = imagecreatefrompng($path);
     	//文字的颜色
         $textcolor = imagecolorallocate($im, 0, 0, 0);
     	//要写的文字
         $str = "文字";
     	//字体位置
         $font = "Public/font/msyh.ttf";
     	//图片的宽度
         $w = imagesx($im);
     	//获取文字长度 根据图片已知宽度乘上一个数值 
         $l = strlen($str)*3;
     	//文字在图片文件中居中
         $position = $w/2 - $l ;
     	//写入文字
         imagettftext($im,12,0,$position,16,$textcolor,$font,$str);
     	//在同样的位置生成同样名称的文件即将源文件替换
         imagepng($im,$path);
     }
    
  • GD 函数网址

原文地址:https://www.cnblogs.com/ikai/p/7531524.html