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
附件名前加网站名
Mar 23 PHP
PHP学习笔记之二 php入门知识
Jan 12 PHP
PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
Nov 10 PHP
Smarty的配置与高级缓存技术分享
Jun 05 PHP
浅析关于PHP位运算的简单权限设计
Jun 30 PHP
PHP生成短网址的3种方法代码实例
Jul 08 PHP
PHP+Mysql+jQuery查询和列表框选择操作实例讲解
Oct 22 PHP
php自定义函数实现二维数组按指定key排序的方法
Sep 29 PHP
php禁用cookie后session设置方法分析
Oct 19 PHP
php 输出json及显示json中的中文汉字详解及实例
Nov 09 PHP
Laravel实现定时任务的示例代码
Aug 10 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安全编程之加密功能
2006/10/09 PHP
使用bcompiler对PHP文件进行加密的代码
2010/08/29 PHP
PHP中防止SQL注入实现代码
2011/02/19 PHP
关于shopex同步ucenter的redirect问题,导致script不运行
2013/04/10 PHP
Eclipse PHPEclipse 配置的具体步骤
2017/08/08 PHP
php图片裁剪函数
2018/10/31 PHP
php获取用户真实IP和防刷机制的实例代码
2018/11/28 PHP
Laravel如何实现自动加载类
2019/10/14 PHP
laravel通过a标签从视图向控制器实现传值
2019/10/15 PHP
JSQL  一个 web DB 的封装
2010/05/05 Javascript
Jquery和JS用外部变量获取Ajax返回的参数值的方法实例(超简单)
2013/06/17 Javascript
仿当当网淘宝网等主流电子商务网站商品分类导航菜单
2013/09/25 Javascript
js中浮点型运算BUG的解决方法说明
2014/01/06 Javascript
中止javascript执行的方法
2014/02/14 Javascript
2014年最火的Node.JS后端框架推荐
2014/10/27 Javascript
浅谈js中的引用和复制(传值和传址)
2016/09/18 Javascript
Node.js包管理器Yarn的入门介绍与安装
2016/10/17 Javascript
js实现打地鼠小游戏
2017/02/13 Javascript
Node.js查找当前目录下文件夹实例代码
2017/03/07 Javascript
微信小程序 蓝牙的实现实例代码
2017/06/27 Javascript
JavaScript无操作后屏保功能的实现方法
2017/07/04 Javascript
jquery+ajaxform+springboot控件实现数据更新功能
2018/01/22 jQuery
Python简单遍历字典及删除元素的方法
2016/09/18 Python
利用python将xml文件解析成html文件的实现方法
2017/12/22 Python
Python3.6笔记之将程序运行结果输出到文件的方法
2018/04/22 Python
Python 读取图片文件为矩阵和保存矩阵为图片的方法
2018/04/27 Python
Python实现合并两个有序链表的方法示例
2019/01/31 Python
如何使用Python多线程测试并发漏洞
2019/12/18 Python
Python爬虫实现HTTP网络请求多种实现方式
2020/06/19 Python
Python 整行读取文本方法并去掉readlines换行\n操作
2020/09/03 Python
JD Sports德国官网:英国领先的运动鞋和运动服饰零售商
2018/02/26 全球购物
shell程序中如何注释
2012/01/28 面试题
2014年教师业务工作总结
2014/12/19 职场文书
2015年党员发展工作总结
2015/05/13 职场文书
2015年基层党建工作汇报材料
2015/06/25 职场文书
浅谈mysql增加索引不生效的几种情况
2021/06/23 MySQL