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 has encountered an Access Violation
Jan 15 PHP
php中file_get_content 和curl以及fopen 效率分析
Sep 19 PHP
PHP ajax 异步执行不等待执行结果的处理方法
May 27 PHP
php实现QQ空间获取当前用户的用户名并生成图片
Jul 25 PHP
Yii实现简单分页的方法
Apr 29 PHP
Yii全局函数用法示例
Jan 22 PHP
php分页查询mysql结果的base64处理方法示例
May 18 PHP
jQuery ajax+PHP实现的级联下拉列表框功能示例
Feb 12 PHP
PHP大文件切割上传功能实例分析
Jul 01 PHP
浅析PHP中的 inet_pton 网络函数
Dec 16 PHP
yii2.0框架场景的简单使用示例
Jan 25 PHP
ThinkPHP5框架中使用JWT的方法示例
Jun 03 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中判断数组相等的方法以及数组运算符介绍
2015/03/30 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
2017/12/25 PHP
PHP工厂模式简单实现方法示例
2018/05/23 PHP
Laravel 6 将新增为指定队列任务设置中间件的功能
2019/08/06 PHP
JS下拉框内容左右移动效果的具体实现
2013/07/10 Javascript
浅析Javascript使用include/require
2013/11/13 Javascript
JS实现简单的图书馆享元模式实例
2015/06/30 Javascript
js实现的全国省市二级联动下拉选择菜单完整实例
2015/08/17 Javascript
jquery实现通用的内容渐显Tab选项卡效果
2015/09/07 Javascript
jquery层级选择器(匹配父元素下的子元素实现代码)
2016/09/05 Javascript
JS Canvas定时器模拟动态加载动画
2016/09/17 Javascript
基于jQuery实现中英文切换导航条效果
2016/09/18 Javascript
深入理解nodejs中Express的中间件
2017/05/19 NodeJs
vue 组件中添加样式不生效的解决方法
2018/07/06 Javascript
vue实现路由切换改变title功能
2019/05/28 Javascript
使用VueCli3+TypeScript+Vuex一步步构建todoList的方法
2019/07/25 Javascript
vue.js实现点击图标放大离开时缩小的代码
2021/01/27 Vue.js
Python中使用ConfigParser解析ini配置文件实例
2014/08/30 Python
python实现删除文件与目录的方法
2014/11/10 Python
浅析Python中else语句块的使用技巧
2016/06/16 Python
Python判断两个对象相等的原理
2017/12/12 Python
python中的json总结
2018/10/11 Python
Python使用GitPython操作Git版本库的方法
2020/02/29 Python
Django 返回json数据的实现示例
2020/03/05 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
2020/07/07 Python
Python+Appium实现自动化清理微信僵尸好友的方法
2021/02/04 Python
世界上最大的隐形眼镜商店:1-800 Contacts
2018/11/03 全球购物
普通PHP程序员笔试题
2016/01/01 面试题
法院实习人员自我鉴定
2013/09/26 职场文书
临床医学专业毕业生的自我评价
2013/10/17 职场文书
机械专业毕业生自荐信
2013/11/02 职场文书
门诊挂号室室长岗位职责
2013/11/27 职场文书
大学生学业生涯规划
2014/01/05 职场文书
2014年庆元旦活动方案
2014/02/15 职场文书
《浅水洼里的小鱼》听课反思
2014/02/28 职场文书
Redis字典实现、Hash键冲突及渐进式rehash详解
2021/09/04 Redis