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学习 字符串课件
Jun 15 PHP
php 不同编码下的字符串长度区分
Sep 26 PHP
谈谈关于php的优点与缺点
Apr 11 PHP
基于PHP开发中的安全防范知识详解
Jun 06 PHP
基于PHP+Ajax实现表单验证的详解
Jun 25 PHP
php中的比较运算符详解
Oct 28 PHP
php的api数据接口书写实例(推荐)
Sep 22 PHP
PHP的消息通信机制测试实例
Nov 10 PHP
PHP实现负载均衡下的session共用功能
Apr 17 PHP
thinkphp5框架扩展redis类方法示例
May 06 PHP
php设计模式之策略模式应用案例详解
Jun 17 PHP
php5与php7的区别点总结
Oct 11 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生成的html meta和link标记在body标签里 顶部有个空行
2010/05/18 PHP
php实现查询百度google收录情况(示例代码)
2013/08/02 PHP
支付宝服务窗API接口开发php版本
2016/07/20 PHP
extjs 学习笔记 四 带分页的grid
2009/10/20 Javascript
js 复制或插入Html的实现方法小结
2010/05/19 Javascript
javascript中有趣的反柯里化深入分析
2012/12/05 Javascript
分享Javascript中最常用的55个经典小技巧
2013/11/29 Javascript
JavaScript对IE操作的经典代码(推荐)
2014/03/10 Javascript
jquery实现倒计时效果
2015/12/14 Javascript
js 求时间差的实现代码
2016/04/26 Javascript
PHP获取当前页面完整URL的方法
2016/12/02 Javascript
vue父子组件的数据传递示例
2017/03/07 Javascript
jQuery实现验证码功能
2017/03/17 Javascript
详解vue父子组件间传值(props)
2017/06/29 Javascript
vuejs实现本地数据的筛选分页功能思路详解
2017/11/15 Javascript
Vue2.0 实现页面缓存和不缓存的方式
2019/11/12 Javascript
用python实现批量重命名文件的代码
2012/05/25 Python
python实现文件名批量替换和内容替换
2014/03/20 Python
Python 实现一个颜色色值转换的小工具
2016/12/06 Python
python自动登录12306并自动点击验证码完成登录的实现源代码
2018/04/25 Python
Python中GeoJson和bokeh-1的使用讲解
2019/01/03 Python
Python列表对象实现原理详解
2019/07/01 Python
Django 实现Admin自动填充当前用户的示例代码
2019/11/18 Python
Pytorch 的损失函数Loss function使用详解
2020/01/02 Python
施华洛世奇加拿大官网:SWAROVSKI加拿大
2018/06/03 全球购物
俄罗斯美容和健康网上商店:Созвездие Красоты
2019/07/23 全球购物
药品质量检测应届生求职信
2013/11/14 职场文书
司机辞职报告范文
2014/01/20 职场文书
座谈会主持词
2014/03/20 职场文书
关于中国梦的演讲稿
2014/04/23 职场文书
语文教师求职信范文
2015/03/20 职场文书
给下属加薪申请报告
2015/05/15 职场文书
2016年第十九届推普周活动总结
2016/04/06 职场文书
vue实现同时设置多个倒计时
2021/05/20 Vue.js
Redis中缓存穿透/击穿/雪崩问题和解决方法
2021/12/04 Redis
世界十大儿童漫画书排名,法国国宝漫画排第五,第二是轰动日本连环
2022/03/18 欧美动漫