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 mb_convert_encoding 获取字符串编码类型实现代码
Apr 26 PHP
同台服务器使用缓存APC效率高于Memcached的演示代码
Feb 16 PHP
php中var_export与var_dump的区别分析
Aug 21 PHP
php输出xml格式字符串(用的这个)
Jul 12 PHP
CI框架安全类Security.php源码分析
Nov 04 PHP
php函数实现判断是否移动端访问
Mar 03 PHP
基于PHP实现简单的随机抽奖小程序
Jan 05 PHP
php+mysql+jquery实现简易的检索自动补全提示功能
Apr 15 PHP
YII2框架中excel表格导出的方法详解
Jul 21 PHP
PHP实现从上往下打印二叉树的方法
Jan 18 PHP
PHP商品秒杀问题解决方案实例详解【mysql与redis】
Jul 22 PHP
tp5.0框架隐藏index.php入口文件及模块和控制器的方法分析
Feb 11 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
IStream与TStream之间的相互转换
2008/08/01 PHP
php 正则匹配函数体
2009/08/25 PHP
PHP获取数组中重复最多的元素的实现方法
2014/11/11 PHP
php基于curl扩展制作跨平台的restfule 接口
2015/05/11 PHP
谈谈PHP连接Access数据库的注意事项
2016/08/12 PHP
php基于协程实现异步的方法分析
2019/07/17 PHP
捕获关闭窗口的脚本
2009/01/10 Javascript
Bookmarklet实现启动jQuery(模仿 云输入法)
2010/09/15 Javascript
javascript 得到文件后缀名的思路及实现
2020/05/09 Javascript
JS实现简洁、全兼容的拖动层实例
2015/05/13 Javascript
Vue.js父与子组件之间传参示例
2017/02/28 Javascript
30分钟快速入门掌握ES6/ES2015的核心内容(下)
2018/04/18 Javascript
react配置antd按需加载的使用
2019/02/11 Javascript
深入解读VUE中的异步渲染的实现
2020/06/19 Javascript
Python中使用logging模块代替print(logging简明指南)
2014/07/09 Python
Python使用matplotlib实现在坐标系中画一个矩形的方法
2015/05/20 Python
小米5s微信跳一跳小程序python源码
2018/01/08 Python
python爬虫爬取快手视频多线程下载功能
2018/02/28 Python
Python 通配符删除文件的实例
2018/04/24 Python
Linux(Redhat)安装python3.6虚拟环境(推荐)
2018/05/05 Python
使用Python AIML搭建聊天机器人的方法示例
2018/07/09 Python
浅谈python常用程序算法
2019/03/22 Python
python3使用腾讯企业邮箱发送邮件的实例
2019/06/28 Python
解决pycharm 工具栏Tool中找不到Run manager.py Task的问题
2019/07/01 Python
Python实现Word表格转成Excel表格的示例代码
2020/04/16 Python
Python自省及反射原理实例详解
2020/07/06 Python
详解python的super()的作用和原理
2020/10/29 Python
CSS3文本换行word-wrap解决英文文本超过固定宽度不换行
2013/10/10 HTML / CSS
白宫黑市官网:White House Black Market
2016/11/17 全球购物
ParcelABC西班牙:包裹运送和快递服务
2019/12/24 全球购物
美国椅子和沙发制造商:La-Z-Boy
2020/10/25 全球购物
META-INF文件夹中的MANIFEST.MF的作用
2016/06/21 面试题
房屋买卖委托书格式范本格式
2014/10/13 职场文书
班级元旦晚会开幕词
2016/03/04 职场文书
3招让你摆脱即兴讲话冷场尴尬
2019/08/08 职场文书
大学生党员暑假实践(活动总结)
2019/08/21 职场文书