这篇文章主要为大家详细介绍了PHP实现往图片上写入文字功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。

网页开发过程中经常遇到需要往素材图片上写入会员信息等参数的场景。这里提供一份PHP代码可以实现此项功能<?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

$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,"
")!== false){

$have = true;

}

if($have){

$words_text = explode("
",$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
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.
Mr. Kang";

$data = array(

'file'=>'20191226152410.png',

'size'=>12,

'angle'=>0,

'fontfile'=>'./Font/ChalkboardSE.ttc',

'width'=>270,

'x'=>20,

'y'=>70

);

//调用方法:

$wordsOnImgObj = new wordsOnImg($data);

$wordsOnImgObj->writeWordsToImg($text);

?>

以上就是PHP实现往图片上写入文字功能的全部内容,希望对大家的学习和解决疑问有所帮助,也希望大家多多支持361模板网。

感谢打赏,我们会为大家提供更多优质资源!

php把图片内容写入文件,PHP实现往图片上写入文字功能相关推荐

  1. UI一揽子计划 18 (沙盒机制、简单对象写入文件、NSFileMange、复杂对象写入文件)

    1. 沙盒机制 数据持久化的原因及本质: 存储在内存中的数据,程序关闭,内存释放,数据丢失,这种数据是临时的.数据持久化是将数据保存成文件,存储到程序的沙盒中. 每个应用程序都有独立的沙盒,就是一个文 ...

  2. Python使用pillow库往图片上写入文字或覆盖另一张图片

    Python使用pillow库往图片上写入文字或覆盖另一张图片 python之pillow是 Python 的第三方图像处理库.pillow是PIL( Python Imaging Library)基 ...

  3. java写入文件编码格式为ansi_Java读取、写入文件如何解决乱码问题

    读取文件流时,经常会遇到乱码的现象,造成乱码的原因当然不可能是一个,这里主要介绍因为文件编码格式而导致的乱码的问题.首先,明确一点,文本文件与二进制文件的概念与差异. 文本文件是基于字符编码的文件,常 ...

  4. python中print又可将数据写入文件_Python第五课-将写入文件的列表格式化

    1.上节课代码中的问题 第四课中介绍了python持久化的基础实例--将数据写入文本文件. 可当我们试着把保存数据的文件读取出来会怎样呢? try: with open('man.txt', 'r') ...

  5. c++ 写入文件_Python学习第52课-写入文件

    [每天几分钟,从零入门python编程的世界!] Python操作外部文件的数据,既然有读取,就会有写入.我们现在学习用Python写入文件. 顺便提一点小小的计算机知识,我们人类读取一个文件,一般是 ...

  6. linux中fstab文件_如何在Linux上写入fstab文件

    linux中fstab文件 zentilia/Shutterstock.comzentilia / Shutterstock.com Adding a new hard drive or solid- ...

  7. Python向hdfs写入文件与hadoop命令向hdfs写入文件

    首先需要安装pyhdfs,可用如下命令安装 pip install pyhdfs 首先我们如果先用hadoop原生命令来操作hdfs(分布式文件系统)上传文件. 命令如下 hadoop fs -put ...

  8. c语言中将结构体写入文件,C语言中将结构体写入文件

    可以使用fwrite()将一个结构体写入文件: fwrite(&some_struct,sizeof somestruct,1,fp); 对应的fread函数可以再把它读出来,此处fwrite ...

  9. 比Everything更强的文件搜索工具,支持文件名、文件内容和文件图片上的文字搜索,文件内容搜索工具,文件图片内容搜索工具,OCR图片文本识别搜索,文件快速搜索工具,文字识别文件搜索工具

    Windows自带的文件搜索功能想必不需要过多吐槽,搜索速度简直是在龟爬,所以小编很早之前就在用Everything进行文件搜索了,不过,今天的主角不是它,而是比它更更更更更强的一款软件! 这款软件适 ...

  10. php 写入sql server,php将图片直接写入SQLServer2008

    SQLServer2008的image字段类型支持将图片.文本文件等直接存入数据库,image类型是以十六进制的格式存储的,所以我们想把图片直接写入数据库,只需要以二进制流的方式读取要存储的图片然后转 ...

最新文章

  1. ecshop /api/client/api.php、/api/client/includes/lib_api.php SQL Injection Vul
  2. 【大脑】改善记忆力的食物有哪些
  3. AngularJS进阶学习
  4. 如何实现分享网站文章到微信朋友圈时显示指定缩略图或LOGO
  5. linux开发板加快开机速度,readahead加速Linux开机速度
  6. PaaS云计算平台服务商大集合
  7. mysql boost 5.7.21_mysql 5.7.21 安装配置方法图文教程(window)
  8. 【转】ABP源码分析二十六:核心框架中的一些其他功能
  9. 国内远程医疗市场快速增长
  10. Bootstrap3 模态对话框的事件
  11. Exchange邮件系统日志查看及管理
  12. 【java学习之路】(java SE篇)008.集合
  13. 推荐: SQL Server Management Express Edition插件
  14. ACM模板——线段树树状数组ST表
  15. (转载)lib 和 dll 的区别、生成以及使用详解
  16. 华为鸿蒙可用型号,华为鸿蒙系统支持手机型号一览
  17. 神舟 桌面显示计算机,神舟(Hasee)笔记本电脑重装系统步骤详解
  18. Win10系统中查看是否开启虚拟化
  19. deflate php,PHP Deflate a string
  20. V831学习日记之串口通信

热门文章

  1. PTA——基础编程题 | 7-13 日K蜡烛图 (15分)
  2. 这种小程序智能客服,你羡慕不?
  3. python爬虫负面新闻_Python爬虫实战项目:简单的百度新闻爬虫
  4. python要求所有浮点数必须带有小数部分_第2章 基本数据类型
  5. 数据结构----线性表应用案例
  6. 如何让人的一生价值最大
  7. 【观察】从客户到合作伙伴,Oracle NetSuite+上上签电子签约背后的三重价值
  8. mysql lock wait_关于MySQL的lock wait timeout exceeded解决方案
  9. Adobe illustrator 2020 fo Mac苹果中文版安装教程
  10. 浏览器分屏技巧,一行代码搞定!!!