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下正则来匹配dede模板标签的代码
Aug 21 PHP
php小偷相关截取函数备忘
Nov 28 PHP
PHP 查找字符串常用函数介绍
Jun 07 PHP
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
Jun 25 PHP
PHP采用get获取url汉字出现乱码的解决方法
Nov 13 PHP
php使用COPY函数更新配置文件的方法
Jun 18 PHP
Laravel SQL语句记录方式(推荐)
May 26 PHP
Thinkphp框架开发移动端接口(2)
Aug 18 PHP
PHP利用超级全局变量$_GET来接收表单数据的实例
Nov 05 PHP
使用php实现网站验证码功能【推荐】
Feb 09 PHP
详解使用php调用微信接口上传永久素材
Apr 11 PHP
利用PHP_XLSXWriter代替PHPExcel的方法示例
Jul 16 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基础知识介绍
2013/09/17 PHP
CodeIgniter扩展核心类实例详解
2016/01/20 PHP
PHP操作mysql数据库分表的方法
2016/06/09 PHP
ExtJS下grid的一些属性说明
2009/12/13 Javascript
JS实现简单的Canvas画图实例
2013/07/04 Javascript
Js日期选择自动填充到输入框(界面漂亮兼容火狐)
2013/08/02 Javascript
jquery清空表单数据示例分享
2014/02/13 Javascript
JavaScript模拟可展开、拖动与关闭的聊天窗口实例
2015/05/12 Javascript
js+html5绘制图片到canvas的方法
2015/06/05 Javascript
使用jquery获取url以及jquery获取url参数的实现方法
2016/05/25 Javascript
JS使用插件cryptojs进行加密解密数据实例
2017/05/11 Javascript
利用Ionic2 + angular4实现一个地区选择组件
2017/07/27 Javascript
vue实现商品加减计算总价的实例代码
2018/08/12 Javascript
node版本管理工具n包使用教程详解
2018/11/09 Javascript
JQuery 实现文件下载的常用方法分析
2019/10/29 jQuery
详解vue或uni-app的跨域问题解决方案
2020/02/21 Javascript
在vue中实现嵌套页面(iframe)
2020/07/30 Javascript
[06:04]DOTA2国际邀请赛纪录片:Just For LGD
2013/08/11 DOTA
Python程序员开发中常犯的10个错误
2014/07/07 Python
Python使用百度API上传文件到百度网盘代码分享
2014/11/08 Python
进一步探究Python的装饰器的运用
2015/05/05 Python
Django处理多用户类型的方法介绍
2019/05/18 Python
Python 硬币兑换问题
2019/07/29 Python
Python单例模式的四种创建方式实例解析
2020/03/04 Python
python输出数学符号实例
2020/05/11 Python
CSS3 Columns分列式布局方法简介
2014/05/03 HTML / CSS
澳大利亚儿童和婴儿产品在线商店:Lime Tree Kids
2017/10/05 全球购物
国贸专业个人求职信分享
2013/12/04 职场文书
《生命的药方》教学反思
2014/04/08 职场文书
优秀学生评语大全
2014/04/25 职场文书
学习十八大的心得体会
2014/09/12 职场文书
公司合作协议范文
2014/10/01 职场文书
基层党员群众路线整改措施及努力方向
2014/10/28 职场文书
2015年财务部工作总结
2015/04/10 职场文书
中学政教处工作总结
2015/08/13 职场文书
Nginx的基本概念和原理
2022/03/21 Servers