PHP实现文字写入图片功能


Posted in PHP onFebruary 18, 2019

本文实例为大家分享了PHP实现文字写入图片的具体代码,供大家参考,具体内容如下

/**
 * PHP实现文字写入图片
 */
class wordsOnImg {
 
  public $config = null;
 
  /**
   * @param $config 传入参数
   * @param $config['file'] 图片文件
   * @param $config['size'] 文字大小
   * @param $config['angle'] 文字的水平角度
   * @param $config['fontfile'] 字体文件路径
   * @param $config['width'] 预先设置的宽度
   * @param $config['x'] 开始写入时的横坐标
   * @param $config['y'] 开始写入时的纵坐标
   */
  public function __construct($config=null){
    if(empty($config)){
      return 'must be config';
    }
    $fileArr = explode(".",$config['file']);
    $config['file_name'] = $fileArr[0];
    $config['file_ext'] = $fileArr[1];
    $this->config = $config;
  }
  /**
   * PHP实现图片上写入实现文字自动换行
   * @param $fontsize 字体大小
   * @param $angle 角度
   * @param $font 字体路径
   * @param $string 要写在图片上的文字
   * @param $width 预先设置图片上文字的宽度
   * @param $flag  换行时单词不折行
   */
  public function wordWrap($fontsize,$angle,$font,$string,$width,$flag=true) {
    $content = "";
    if($flag){
      $words = explode(" ",$string);
      foreach ($words as $key=>$value) {
        $teststr = $content." ".$value;
        $testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
        //判断拼接后的字符串是否超过预设的宽度
        if(($testbox[2] > $width)) {
          $content .= "\n";
        }
        $content .= $value." ";
      }
    }else{
      //将字符串拆分成一个个单字 保存到数组 letter 中
      for ($i=0;$i<mb_strlen($string);$i++) {
        $letter[] = mb_substr($string, $i, 1);
      }
      foreach ($letter as $l) {
        $teststr = $content." ".$l;
        $testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
        // 判断拼接后的字符串是否超过预设的宽度
        if (($testbox[2] > $width) && ($content !== "")) {
          $content .= "\n";
        }
        $content .= $l;
      }
    }
    return $content;
  }
 
  /**
   * 实现写入图片
   * @param $text 要写入的文字
   * @param $flag 是否直接输出到浏览器,默认是
   */
  public function writeWordsToImg($text,$flag=true){
    if(empty($this->config)){
      return 'must be config';
    }
    //获取图片大小
    $img_pathWH = getimagesize($this->config['file']);
    //打开指定的图片文件
    $im = imagecreatefrompng($this->config['file']);
    #设置水印字体颜色
    $color = imagecolorallocatealpha($im,0, 0, 255, 75);//蓝色
    $have = false;
    if(stripos($text,"<br/>")!== false){
      $have = true;
    }
    if($have){
      $words_text = explode("<br/>",$text);
      $words_text[0] = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $words_text[0], $this->config['width']); //自动换行处理
      $words_text[1] = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $words_text[1], $this->config['width']); //自动换行处理
      $words_text[2] = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $words_text[2], $this->config['width']); //自动换行处理
      imagettftext($im, $this->config['size'], $this->config['angle'], $this->config['x'], $this->config['y'], $color, $this->config['fontfile'], $words_text[0]);
      imagettftext($im, $this->config['size'], $this->config['angle'], $this->config['x'], $this->config['y']+30, $color, $this->config['fontfile'], "  ".$words_text[1]);
      imagettftext($im, $this->config['size'], $this->config['angle'], $img_pathWH[0]/2+70, $img_pathWH[1]-80, $color, $this->config['fontfile'], $words_text[2]);
      if($flag){
        header("content-type:image/png");
        imagepng($im);
        imagedestroy($im);
      }
      imagepng($im,$this->config['file_name'].'_1.'.$this->config['file_ext']);
      imagedestroy($im);
    }
    $words_text = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $text, $this->config['width']); //自动换行处理
    imagettftext($im, $this->config['size'], $this->config['angle'], $this->config['x'], $this->config['y'], $color, $this->config['fontfile'], $words_text);
    if($flag){
      header("content-type:image/png");
      imagepng($im);
      imagedestroy($im);
    }
    imagepng($im,$this->config['file_name'].'_1.'.$this->config['file_ext']);
    imagedestroy($im);
  }
}
 
$text = "Dear Kang<br/>If you can hold something up and put it down, it is called weight lifting;if you can hold something up but can never put it down, it's called bueden bearing. Pitifully, most of people are bearing heavy burdens when they are in love.\n\nBeing nice to someone you dislike doesn't mean you're a hypocritical people. It means you're mature enough to tolerate your dislike towards them.<br/>Mr. Kang";
 
$data = array(
  'file'=>'20171226152410.png',
  'size'=>12,
  'angle'=>0,
  'fontfile'=>'./Font/ChalkboardSE.ttc',
  'width'=>270,
  'x'=>20,
  'y'=>70
);
//使用
$wordsOnImgObj = new wordsOnImg($data);
$wordsOnImgObj->writeWordsToImg($text);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
PHP+XML 制作简单的留言本 图文教程
Nov 02 PHP
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
Jun 16 PHP
php cc攻击代码与防范方法
Oct 18 PHP
PHP加密扩展库Mcrypt安装和实例
Nov 10 PHP
php实现httpRequest的方法
Mar 13 PHP
文件上传之SWFUpload插件(代码)
Jul 30 PHP
php实现简单的上传进度条
Nov 17 PHP
php实现网页端验证码功能
Jul 11 PHP
PHP单例模式模拟Java Bean实现方法示例
Dec 07 PHP
实例讲解php实现多线程
Jan 27 PHP
PHP关于foreach复制知识点总结
Jan 28 PHP
PHP数据对象映射模式实例分析
Mar 29 PHP
php分享朋友圈的实现代码
Feb 18 #PHP
php微信分享到朋友圈、QQ、朋友、微博
Feb 18 #PHP
php实现微信分享朋友链接功能
Feb 18 #PHP
PHP实现唤起微信支付功能
Feb 18 #PHP
thinkphp5使用无限极分类
Feb 18 #PHP
thinkphp5实现无限级分类
Feb 18 #PHP
php实现文章评论系统
Feb 18 #PHP
You might like
php 分库分表hash算法
2009/11/12 PHP
php中计算时间差的几种方法
2009/12/31 PHP
PHP获取文本框、密码域、按钮的值实例代码
2017/04/19 PHP
如何使用jQUery获取选中radio对应的值(一句代码)
2013/06/03 Javascript
Javascript alert消息换行的方法
2013/08/07 Javascript
jqgrid 表格数据导出实例
2013/11/21 Javascript
JavaScript中的return语句简单介绍
2015/12/07 Javascript
js表单验证实例讲解
2016/03/31 Javascript
js中遍历对象的属性和值的方法
2016/07/27 Javascript
JSON与String互转的实现方法(Javascript)
2016/09/27 Javascript
js实现倒计时及时间对象
2016/11/15 Javascript
Extjs让combobox写起来简洁又漂亮
2017/01/05 Javascript
JavaScript获取当前时间向前推三个月的方法示例
2017/02/04 Javascript
React 使用recharts实现散点地图的示例代码
2018/12/07 Javascript
js时间戳转yyyy-MM-dd HH-mm-ss工具类详解
2019/04/30 Javascript
微信小程序事件对象中e.target和e.currentTarget的区别详解
2019/05/08 Javascript
如何用原生js写一个弹窗消息提醒插件
2019/05/24 Javascript
vue 解决文本框被键盘遮住的问题
2019/11/06 Javascript
微信小程序swiper使用网络图片不显示问题解决
2019/12/13 Javascript
react antd表格中渲染一张或多张图片的实例
2020/10/28 Javascript
Django中使用group_by的方法
2015/05/26 Python
Python字符串转换成浮点数函数分享
2015/07/24 Python
快速入手Python字符编码
2016/08/03 Python
django基础之数据库操作方法(详解)
2017/05/24 Python
解决pandas read_csv 读取中文列标题文件报错的问题
2018/06/15 Python
Python定时任务工具之APScheduler使用方式
2019/07/24 Python
Python使用Pandas库常见操作详解
2020/01/16 Python
Python实现自动装机功能案例分析
2020/10/22 Python
Python 的 f-string 可以连接字符串与数字的原因解析
2021/02/20 Python
blueseventy官网:铁人三项和比赛泳衣
2021/02/06 全球购物
主持人演讲稿范文
2013/12/28 职场文书
信息科学与技术专业求职信范文
2014/02/20 职场文书
安全责任书怎么写
2014/07/28 职场文书
2015年社区精神文明工作总结
2015/05/26 职场文书
pandas 实现将NaN转换为None
2021/05/14 Python
详解Java实践之抽象工厂模式
2021/06/18 Java/Android