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实现文字水印图片
php实现简单的文字水印图片,使用前需要开启php配置中的gd2功能 <?php/*打开图片*/ //1.配置图片路径 $src="image/55.jpg";//这个路径改 ...
- Swift - UIPasteboard剪贴板的使用详解(复制、粘贴文字和图片)
转载自:http://www.hangge.com/blog/cache/detail_1085.html UITextField.UITextView组件系统原生就支持文字的复制,但有时我们需要让其 ...
- 开发笔记:PDF生成文字和图片水印
背景 团队手里在做的一个项目,其中一个小功能是用户需要上传PDF文件到文件服务器上,都是一些合同或者技术评估文档,鉴于知识版权和防伪的目的,需要在上传的PDF文件打上水印, 这时候我们需要提供能力给客 ...
- C#操作word模板插入文字、图片及表格详细步骤
c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...
- CSS3动画:流彩文字效果+图片模糊效果+边框伸展效果实现
前言 首先第一步,先布局html代码如下: <div class="wrap"> <img src="images/1.jpg" class= ...
- 分别使用docx4j,jacob将文字与图片插入word中书签位置
项目中需要将一段文字,与人员的签名(图片)插入到上传的word中,上网查询了一下,有许多种方式可以向word中插入文字,发现docx4j与jacob都为比较常见的解决方案,于是就先使用的docx4j进 ...
- wps如何设置文字环绕图片
wps在编辑一些文字的时候,经常会插入一些图片,但是插入图片后,文字和图片就被分离开来,整体显得没有那么美观整洁,这个时候就用到了软件的文字环绕功能,那么具体如何设置呢,接下来看教程. 首先打开wps ...
- Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博
原文:Android 调用系统分享文字.图片.文件,可直达微信.朋友圈.QQ.QQ空间.微博 兼容SDK 18以上的系统,直接调用系统分享功能,分享文本.图片.文件到第三方APP,如:微信.QQ.微博 ...
- ASP.NET(C#)图片加文字、图片水印,神啊,看看吧
ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...
随机推荐
- 记一个简单的webpack.config.js
module.exports = { entry: './basic/app.js', output: { path: './assets/', filename: '[name].bundle.js ...
- Linux下统计某个目录的文件个数(转)
1.统计某文件夹下文件个数,不包括子文件夹 比如:统计/home下.jpeg文件的个数 ls -l "/home" | grep ".jpeg" | wc -l ...
- 通过JS的事件处理取得radio的值
转自:http://blog.sina.com.cn/s/blog_50a1e17401017pik.html 提前知识准备: 在一个HTML文档中,每个元素都可以设置ID和NAME属性. 其中ID属 ...
- Unity Update 具体解释
0x01:简单介绍 Unity的脚本继承了Monobehaviour类,在脚本中定义函数: void FixedUpdate(){} void Update(){} void LateUpdate() ...
- 用protractor測试canvas绘制(二)
上一篇写了通过webdriver在浏览器环境下异步调用js代码. 今天进入正题. 事实上有了executeAsyncScript,一切就呼之欲出了. 直接上代码: var compareImage=f ...
- C语言函数--H
函数名: harderr 功 能: 建立一个硬件错误处理程序 用 法: void harderr(int (*fptr)()); 程序例: /*This program will trap disk ...
- requireJS使用shim注入非标准模块详解
在javascript中定义全局变量有2种方式,本质上是等价的,都是向window对象注入属性或者方法. // global.js var g_name = "aty"; wind ...
- luogu1415 拆分数列
题目大意 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输出使得最后一个数最小的同时,字典序最大的解(即先要满足最后一个数最小:如果有多组解,则使得第一个数尽量大:如 ...
- [Codeforces 1013B] And
[题目链接] http://codeforces.com/problemset/problem/1013/B [算法] 不难发现,答案只有0,1,2,-1,共4种取值 分类讨论即可,计算时可以使用ST ...
- C语言程序创建文件
#include <stdio.h>#include <stdlib.h>int main() { FILE *fp;if((fp=fopen("g:\\a.txt& ...