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 相关文章推荐
如何实现给定日期的若干天以后的日期
Oct 09 PHP
PHP聊天室技术
Oct 09 PHP
php5 and xml示例
Nov 22 PHP
PHP 身份验证方面的函数
Oct 11 PHP
PHP Socket 编程
Apr 09 PHP
PHP关联链接常用代码
Nov 05 PHP
php漏洞之跨网站请求伪造与防止伪造方法
Aug 15 PHP
PHP zip扩展Linux下安装过程分享
May 05 PHP
php给每个段落添加空格的方法
Mar 20 PHP
PHP实现搜索地理位置及计算两点地理位置间距离的实例
Jan 08 PHP
PHP使用Redis替代文件存储Session的方法
Feb 15 PHP
PHP实现的无限分类类库定义与用法示例【基于thinkPHP】
Aug 06 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 全文搜索和替换的实现代码
2008/07/29 PHP
一贴学会PHP 新手入门教程
2009/08/03 PHP
php采用file_get_contents代替使用curl实例
2014/11/07 PHP
PHP扩展模块memcached长连接使用方法分析
2014/12/24 PHP
ThinkPHP和UCenter接口冲突的解决方法
2016/07/25 PHP
JavaScript使用prototype定义对象类型
2007/02/07 Javascript
jQuery经过一段时间自动隐藏指定元素的方法
2015/03/17 Javascript
如何使用jQuery技术开发ios风格的页面导航菜单
2015/07/29 Javascript
详解JavaScript基于面向对象之创建对象(2)
2015/12/10 Javascript
原生js封装二级城市下拉列表的实现代码
2016/06/16 Javascript
AngularJS教程之环境设置
2016/08/16 Javascript
javascript 分号总结及详细介绍
2016/09/24 Javascript
利用canvas中toDataURL()将图片转为dataURL(base64)的方法详解
2017/11/20 Javascript
vue-router传参用法详解
2019/01/19 Javascript
vue 动态设置img的src地址无效,npm run build 后找不到文件的解决
2020/07/26 Javascript
vue实现一个矩形标记区域(rectangle marker)的方法
2020/10/28 Javascript
vue基于Echarts的拖拽数据可视化功能实现
2020/12/04 Vue.js
python使用any判断一个对象是否为空的方法
2014/11/19 Python
Python标准库之多进程(multiprocessing包)介绍
2014/11/25 Python
使用Python发送各种形式的邮件的方法汇总
2015/11/09 Python
python中os模块详解
2016/10/14 Python
Python版名片管理系统
2018/11/30 Python
利用nohup来开启python文件的方法
2019/01/14 Python
基于Python的PIL库学习详解
2019/05/10 Python
Django时区详解
2019/07/24 Python
python3 使用Opencv打开USB摄像头,配置1080P分辨率的操作
2019/12/11 Python
Python实现搜索算法的实例代码
2020/01/02 Python
Python动态导入模块和反射机制详解
2020/02/18 Python
python获取栅格点和面值的实现
2020/03/10 Python
在Matplotlib图中插入LaTex公式实例
2020/04/17 Python
使用gunicorn部署django项目的问题
2020/12/30 Python
在html页面中取得session中的值的方法
2020/08/11 HTML / CSS
大学生个人求职信范文
2013/09/21 职场文书
上甘岭观后感
2015/06/10 职场文书
浅谈TypeScript 索引签名的理解
2021/10/16 Javascript
MySQL表锁、行锁、排它锁及共享锁的使用详解
2022/04/02 MySQL