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 相关文章推荐
第三节 定义一个类 [3]
Oct 09 PHP
解析CI的AJAX分页 另类实现方法
Jun 27 PHP
PHP按行读取文件时删除换行符的3种方法
May 04 PHP
查找php配置文件php.ini所在路径的二种方法
May 26 PHP
PHP实现指定字段的多维数组排序函数分享
Mar 09 PHP
php实现简单的MVC框架实例
Sep 23 PHP
CentOS下搭建PHP环境与WordPress博客程序的全流程总结
May 07 PHP
Docker 如何布置PHP开发环境
Jun 21 PHP
AJAX的使用方法详解
Apr 29 PHP
php微信开发之关键词回复功能
Jun 13 PHP
PHP7内核之Reference详解
Mar 14 PHP
PHP7修改的函数
Mar 09 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
Thinkphp的volist标签嵌套循环使用教程
2014/07/08 PHP
Yii实现单用户博客系统文章详情页插入评论表单的方法
2015/12/28 PHP
PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法
2016/01/07 PHP
PHP5.3新特性小结
2016/02/14 PHP
php把字符串指定字符分割成数组的方法
2018/03/12 PHP
laravel框架中视图的基本使用方法分析
2019/11/23 PHP
js 跨域和ajax 跨域问题小结
2009/07/01 Javascript
window.dialogArguments 使用说明
2011/04/11 Javascript
JavaScript插入动态样式实现代码
2012/02/22 Javascript
javascript算法题 求任意一个1-9位不重复的N位数在该组合中的大小排列序号
2012/07/21 Javascript
一行代码实现纯数据json对象的深度克隆实现思路
2013/01/09 Javascript
js弹出div并显示遮罩层
2014/02/12 Javascript
微信小程序视图template模板引用的实例详解
2017/09/20 Javascript
vue-awesome-swiper滑块插件使用方法详解
2017/11/27 Javascript
js实现手机web图片左右滑动效果
2017/12/29 Javascript
利用Angular2 + Ionic3开发IOS应用实例教程
2018/01/15 Javascript
微信小程序实现点击卡片 翻转效果
2019/09/04 Javascript
js+canvas实现画板功能
2020/09/13 Javascript
vue实现单一筛选、删除筛选条件
2020/10/26 Javascript
[10:07]2014DOTA2国际邀请赛 实拍选手现场观战DK对阵Titan
2014/07/12 DOTA
使用Python的Bottle框架写一个简单的服务接口的示例
2015/08/25 Python
python 实现删除文件或文件夹实例详解
2016/12/04 Python
Pycharm+Scrapy安装并且初始化项目的方法
2019/01/15 Python
python super的使用方法及实例详解
2019/09/25 Python
将labelme格式数据转化为标准的coco数据集格式方式
2020/02/17 Python
python的dict判断key是否存在的方法
2020/12/09 Python
最便宜促销价格订机票:Airpaz(总部设在印尼,支持中文)
2018/11/13 全球购物
英国Iceland杂货店:网上食品购物
2020/12/16 全球购物
珍惜水资源建议书
2014/03/12 职场文书
产品销售计划书
2014/05/04 职场文书
班级活动总结格式
2014/08/30 职场文书
授权委托书样本
2014/09/25 职场文书
幼儿园教师安全责任书
2015/05/08 职场文书
《风筝》教学反思
2016/02/23 职场文书
2019年二手房买卖合同范本
2019/10/14 职场文书
十大最强火系宝可梦,喷火龙上榜,第一名有双火属性
2022/03/18 日漫