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实现登陆验证码(类似条行码状)
Oct 09 PHP
php中一个有意思的日期逻辑处理
Mar 25 PHP
CI(CodeIgniter)框架介绍
Jun 09 PHP
destoon实现不同会员组公司名称显示不同的颜色的方法
Aug 22 PHP
完美实现wordpress禁止文章修订和自动保存的方法
Nov 03 PHP
Laravel 5框架学习之表单验证
Apr 08 PHP
详解WordPress中调用评论模板和循环输出评论的PHP函数
Jan 05 PHP
Android App中DrawerLayout抽屉效果的菜单编写实例
Mar 21 PHP
php自定义扩展名获取函数示例
Dec 12 PHP
PHP中Laravel 关联查询返回错误id的解决方法
Apr 01 PHP
laravel-admin 实现给grid的列添加行数序号的方法
Oct 08 PHP
phpinfo的知识点总结
Oct 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
在浏览器窗口上添加遮罩层的方法
2012/11/12 Javascript
解决JQeury显示内容没有边距内容紧挨着浏览器边线
2013/12/20 Javascript
有效提高JavaScript执行效率的几点知识
2015/01/31 Javascript
javascript实现删除前弹出确认框
2015/06/04 Javascript
跟我学习javascript的this关键字
2020/05/28 Javascript
javascript+css3 实现动态按钮菜单特效
2016/02/06 Javascript
简单谈谈javascript中this的隐式绑定
2016/02/22 Javascript
noty ? jQuery通知插件全面解析
2016/05/18 Javascript
JavaScript直播评论发弹幕切图功能点集合效果代码
2016/06/26 Javascript
js监听input输入框值的实时变化实例
2017/01/26 Javascript
jQuery操作之效果详解
2017/05/19 jQuery
Vue单页及多页应用全局配置404页面实践记录
2018/05/22 Javascript
详解Angular模板引用变量及其作用域
2018/11/23 Javascript
Nodejs libuv运行原理详解
2019/08/21 NodeJs
vue 路由meta 设置导航隐藏与显示功能的示例代码
2020/09/04 Javascript
Javascript新手入门之字符串拼接与变量的应用
2020/12/03 Javascript
[01:15:00]LGD vs Mineski Supermajor 胜者组 BO3 第一场 6.5
2018/06/06 DOTA
python脚本生成caffe train_list.txt的方法
2018/04/27 Python
python3.x 将byte转成字符串的方法
2018/07/17 Python
python中报错&quot;json.decoder.JSONDecodeError: Expecting value:&quot;的解决
2019/04/29 Python
详解python对象之间的交互
2020/09/29 Python
css3 border旋转时的动画应用
2016/01/22 HTML / CSS
基础的CSS3弹性盒Flexbox布局使用实例
2016/04/08 HTML / CSS
印度首选时尚目的地:Reliance Trends
2018/01/17 全球购物
泰国排名第一的家居用品中心:HomePro
2020/11/18 全球购物
编写函数,将一个3*3矩阵转置
2013/10/09 面试题
请写出char *p与"零值"比较的if语句
2014/09/24 面试题
银行见习期自我鉴定
2014/01/29 职场文书
元旦晚会邀请函
2014/02/01 职场文书
初中同学会活动方案
2014/08/22 职场文书
县委常委班子专题民主生活会查摆问题及整改措施
2014/09/27 职场文书
2014年世界艾滋病日宣传活动总结
2014/11/18 职场文书
好媳妇事迹材料
2014/12/24 职场文书
毕业生对母校寄语
2015/02/26 职场文书
2014年度个人总结范文
2015/03/09 职场文书
2016幼儿园新学期寄语
2015/12/03 职场文书