/**
* 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实现文字写入图片功能的更多相关文章

  1. PHP实现文字水印图片

    php实现简单的文字水印图片,使用前需要开启php配置中的gd2功能 <?php/*打开图片*/ //1.配置图片路径 $src="image/55.jpg";//这个路径改 ...

  2. Swift - UIPasteboard剪贴板的使用详解(复制、粘贴文字和图片)

    转载自:http://www.hangge.com/blog/cache/detail_1085.html UITextField.UITextView组件系统原生就支持文字的复制,但有时我们需要让其 ...

  3. 开发笔记:PDF生成文字和图片水印

    背景 团队手里在做的一个项目,其中一个小功能是用户需要上传PDF文件到文件服务器上,都是一些合同或者技术评估文档,鉴于知识版权和防伪的目的,需要在上传的PDF文件打上水印, 这时候我们需要提供能力给客 ...

  4. C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  5. CSS3动画:流彩文字效果+图片模糊效果+边框伸展效果实现

    前言 首先第一步,先布局html代码如下: <div class="wrap"> <img src="images/1.jpg" class= ...

  6. 分别使用docx4j,jacob将文字与图片插入word中书签位置

    项目中需要将一段文字,与人员的签名(图片)插入到上传的word中,上网查询了一下,有许多种方式可以向word中插入文字,发现docx4j与jacob都为比较常见的解决方案,于是就先使用的docx4j进 ...

  7. wps如何设置文字环绕图片

    wps在编辑一些文字的时候,经常会插入一些图片,但是插入图片后,文字和图片就被分离开来,整体显得没有那么美观整洁,这个时候就用到了软件的文字环绕功能,那么具体如何设置呢,接下来看教程. 首先打开wps ...

  8. Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博

    原文:Android 调用系统分享文字.图片.文件,可直达微信.朋友圈.QQ.QQ空间.微博 兼容SDK 18以上的系统,直接调用系统分享功能,分享文本.图片.文件到第三方APP,如:微信.QQ.微博 ...

  9. ASP.NET(C#)图片加文字、图片水印,神啊,看看吧

    ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...

随机推荐

  1. spring boot开发REST接口

    1.配置pom.xml文件的<parent>和<depencencies>,指定spring boot web依赖 <parent> <groupId> ...

  2. asp.net--webservice--安全验证SoapHeader

    服务端的设置 客户端设置,需要生成一个服务端的代理,调用服务端的方法

  3. mongodb之配置

    前言 最新版本支持yaml格式的配置文件,只支持空格,不能使用tab 详细配置说明 #系统日志配置 systemLog: destination: file path: /var/log/mongod ...

  4. React Native Mac配置指南

    步骤 http://facebook.github.io/react-native/docs/getting-started.html#content 依照React Native官网步骤一步步安装, ...

  5. 微信被动回复用户消息-文本消息-springmvc环境下自动生成xml

    微信被动回复用户消息-文本消息-springmvc环境下自动生成xml springmvc - 大牛! private Object subscribeMessage(Scan scan) { Sca ...

  6. poj 2931 Building a Space Station &lt;克鲁斯卡尔&gt;

    Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5869 Accepted: 2 ...

  7. C# winform窗体在桌面右下角显示(任务栏上方)

    问题描述: 有一个主窗口程序,需要给该程序添加一个通知子窗口.子窗口的位置为右下角. 解决方法: 在子窗口frmPopMsg的代码文件中添加如下代码: public frmPopMsg() { Ini ...

  8. C语言相关

    标准输入输出 格式化输入输出 int a,b; int arr[10]={1},*p=&b; double d; char ch,str[30]; scanf("%d%d" ...

  9. 两个向量之间的欧式距离及radial-basis-functions(RBF)

    template <class DataType1, class DataType2>double EuclideanDistance(std::vector<DataType1&g ...

  10. C#(服务器)与Java(客户端)通过Socket传递对象(序列化 json)

    下面详细讲解实现的关键步骤:          通信关键: C#和java用Socket通信,发送数据和接收数据可以统一采用UTF-8编码,经过测试,使用UTF-8编码可以成功传递对象. 对于Sock ...