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
PHP新手上路(十一)
Oct 09 PHP
PHP simple_html_dom.php+正则 采集文章代码
Dec 24 PHP
Zend framework处理一个http请求的流程分析
Feb 08 PHP
浅谈php扩展imagick
Jun 02 PHP
php模仿asp Application对象在线人数统计实现方法
Jan 04 PHP
php实现粘贴截图并完成上传功能
May 17 PHP
PHP使用内置函数生成图片的方法详解
May 09 PHP
php创建图像具体步骤
Mar 13 PHP
php生成无限栏目树
Mar 16 PHP
详解PHP神奇又有用的Trait
Mar 25 PHP
PHP+Mysql分布式事务与解决方案深入理解
Feb 27 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读取富文本的时p标签会出现红线是怎么回事
2014/05/13 PHP
jQuery Flash/MP3/Video多媒体插件
2010/01/18 Javascript
javascript scrollTop正解使用方法
2013/11/14 Javascript
jQuery中:text选择器用法实例
2015/01/03 Javascript
基于dropdown.js实现的两款美观大气的二级导航菜单
2015/09/02 Javascript
JS实现图片高亮展示效果实例
2015/11/24 Javascript
JS常用知识点整理
2017/01/21 Javascript
WdatePicker.js时间日期插件的使用方法
2017/07/26 Javascript
JavaScript基础之流程控制语句的用法
2017/08/31 Javascript
js读取本地文件的实例
2017/12/22 Javascript
NodeJS搭建HTTP服务器的实现步骤
2018/10/12 NodeJs
浏览器事件循环与vue nextTicket的实现
2019/04/16 Javascript
node.js 微信开发之定时获取access_token
2020/02/07 Javascript
如何通过JS实现转码与解码
2020/02/21 Javascript
解决vue单页面 回退页面 keeplive 缓存问题
2020/07/22 Javascript
使用vue引入maptalks地图及聚合效果的实现
2020/08/10 Javascript
vue自定义指令限制输入框输入值的步骤与完整代码
2020/08/30 Javascript
python实现复制整个目录的方法
2015/05/12 Python
利用python编写一个图片主色转换的脚本
2017/12/07 Python
Python小整数对象池和字符串intern实例解析
2020/03/21 Python
django正续或者倒序查库实例
2020/05/19 Python
opencv 阈值分割的具体使用
2020/07/08 Python
PyTorch中clone()、detach()及相关扩展详解
2020/12/09 Python
英国日常交易网站:Wowcher
2018/09/04 全球购物
瑞士图书网站:Weltbild.ch
2019/09/17 全球购物
SEPHORA丝芙兰德国官方购物网站:化妆品、护肤品和香水
2020/01/21 全球购物
既然说Ruby中一切都是对象,那么Ruby中类也是对象吗
2013/01/26 面试题
幼儿园中班开学寄语
2014/04/03 职场文书
讲解员培训方案
2014/05/04 职场文书
党员四风自我剖析材料思想汇报
2014/09/13 职场文书
党性分析自查总结
2014/10/14 职场文书
单方离婚协议书范本2014
2014/10/28 职场文书
仓管员岗位职责
2015/02/03 职场文书
国富论读书笔记
2015/06/26 职场文书
预备党员入党感言
2015/08/01 职场文书
详解Nginx 工作原理
2021/03/31 Servers