粗略写了使用GD2制作文字图像demo
项目要求宽,高为传入参数;文字大小,文字间隔需要自动调节;
由于imagettftext()函数写入文字坐标点不以画布左上角为原点,而是根据文字的字体类型,字体大小,中英文,标点等因素变换(测试多组数据,Y轴0坐标点位于一个字体宽度左右位置),所以文字居中问题还未得到解决;
由于文字坐标
<?php
class Index
{
private $config = array(
'width' => '160',
'height' => '90',
'frame' => '0.05',
// 默认背景色红色,字体白色
'imageColor' => array(220,20,60,0),
'fontColor' => array(255,255,255,0),
'extend' => array(
'fontType' => 'c:/windows/fonts/simhei.ttf', // 字体类型
),
);
public function newimage($text = '测试',$options = array())
{
header("Content-type: text/html; charset=utf-8");
$options = $_GET;
$text = $_GET['text'];
$this->config = array_merge($this->config,$options);
$location = array(0,0,0);
$imageColor = $this->config['imageColor'];
$fontColor = $this->config['fontColor'];
$width = $this->config['width'];
$height = $this->config['height'];
//处理中英,标点计算字符长度
$strlength = $this->utf8_strlen($text);
// 计算边框长度
$padding = $this->config['frame'];
if( $padding < 0.5 )
{
// 百分比 以短边为标准计算padding
$padding = $height*$padding*2;
}
else
{
$padding = $padding*2;
}
// 去掉边框的有效长度
$width_1 = $width-$padding;
$height_1 = $height-$padding;
// 判断单个字体长度和高的大小
if( $strlength >=1 )
{
$oneBox = $width_1/$strlength;
}
else
{
exit('字符不能为空');
}
if( $oneBox > $height_1 )
{
$oneBox = $height_1;
if( $strlength <=1 )
{
// 一个字符
$gap = 0;
$location[1] = ($width - $oneBox)/2;
}
else
{
// 计算横向间距
$gap = ($width_1 - ($oneBox*$strlength))/($strlength-1);
$location[1] = $padding/2;
}
$location[2] = ($padding/2)+$oneBox;
// $location[2] = ($height-($padding/2)+$oneBox)*0.5;
}
else
{
$gap = 0;
$location[1] = $padding/2;
// 计算纵向坐标Y
$location[2] = (($height-$oneBox)/2)+$oneBox;
// $location[2] = ($height-($padding/2)+$oneBox)*0.5;
}
// 字体磅数计算
$size = 72*$oneBox/96;
// 分割中文字体为数组
$fontArr = preg_split('/(?<!^)(?!$)/u', $text );
// 调试
if($_GET['debug'] === 'true')
{
print_r('字符长度:'.$strlength . '<br/>');
print_r('字符磅数:'.$size . '<br/>');
print_r('单个字符:' . $oneBox . '<br/>');
print_r('边距:' . $padding/2 . '<br/>');
print_r('去掉边框的高度:' . $height_1 . '<br/>');
var_dump($fontArr);
exit();
}
$font = $this->config['extend']['fontType'];
//创建空白图片的长 、高
$img = imagecreate($width, $height);
//给图片分配颜色
imagecolorallocatealpha($img, $imageColor[0], $imageColor[1], $imageColor[2] ,$imageColor[3]);
//设置字体颜色
$color = imagecolorallocatealpha($img, $fontColor[0], $fontColor[1], $fontColor[2] ,$fontColor[3]);
//将ttf文字写到图片中
foreach ($fontArr as $key => $value)
{
imagettftext($img, $size, $location[0], $location[1]+($oneBox+$gap)*($key), $location[2], $color, $font, $value);
// imagestring($img, 5, $location[1]+($oneBox+$gap)*($key), $location[2], $value,$color);
}
//发送头信息
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
}
// 计算中文字符串长度
public function utf8_strlen($string = null)
{
// 将字符串分解为单元
preg_match_all("/./us", $string, $match);
// 返回单元个数
return count($match[0]);
}
}
$obj = new Index();
$obj->newimage();
粗略写了使用GD2制作文字图像demo的更多相关文章
- cocos2d-x 利用CCLabelTTF制作文字描边与阴影效果的实现方法
// // myttf.h// // Created by 王天宇 on 14-6-12. // // #ifndef ____SLG__myttf__ #define ____SLG__myttf_ ...
- 利用Adorner制作用于图像裁切的选择框
原文:利用Adorner制作用于图像裁切的选择框 前天,我写了一篇"使用Adorner显示WPF控件的边界点"的文章.这次,使用从Adorner继承来写一个用于图像裁切的选择框. ...
- dreamweaver中的 map怎么调用?_制作热点图像区域
我们浏览网页时,经常看到一些图片上会出现特别的超链接,即在一张图片上有多个局部区域和不同的网页链接,比如地图链接. 这就是映射图像(Image Map),它是指一幅根据链接对象不同而被人为划分为若干指 ...
- HTML5火焰文字特效DEMO演示
效果展示:http://hovertree.com/texiao/html5/26/ 效果图: 扫描二维码查看效果:
- HTML5火焰文字特效DEMO演示---转载
只有google支持 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- PHP中应用GD2函数在图像上添加文字
<?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/g ...
- [2014.01.27]wfTextImage 文字图像组件 1.6
全新开发的文字转图像组件--wfTextImage,使用简单,功能强大,图像处理效果极佳. 将大段的文本内容转换成GIF图片. 有效防止文字内容被复制抄袭,有效保护文字资料. ...
- 【iOS发展-28】制造业UITabBarController标记控制器、定制UITabBarItem文字图像6途径和More评论
一个.一个简单的制作过程(实际工程中不建议这样的方式,不要只展示所用原理的理解) 在AppDelegate.m在: - (BOOL)application:(UIApplication *)appli ...
- Quart2D文字图像绘制
上一个是绘制简单图形,这一篇学习绘制文字.图像 //获取画布 CGContextRef context=UIGraphicsGetCurrentContext(); //设置边框颜色 CGContex ...
随机推荐
- set()和get()方法
在很多程序中,都喜欢定义一个privata变量,然后为这个私有变量加上get(),set()方法.那为什么不直接定义一个public变量呢?这样做到底有什么好处和意义呢?难道真的仅仅只是为了代码规范? ...
- 【剑指Offer】05、用两个栈实现队列
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题解一: //stack2有元素就pop,没有元素就将stack1中所有元素倒进来再pop public ...
- 在服务器上安装并配置JDK1.8
参考链接:https://blog.csdn.net/qq_40958000/article/details/83996912
- Missing artifact com.oracle:ojdbc14:jar:10.2.0.1.0
问题说明:导入Maven项目时,想要添加Oracle驱动包时,Maven的pom.xml总是报Missing artifact com.oracle:ojdbc14:jar:10.2.0.1.0错. ...
- NODEJS 使用 sqlite3 本地文件数据库
npm install sqlite3 var sqlite3 = require('sqlite3').verbose();var db = new sqlite3.Database('WebFil ...
- Redis-03-集群搭建
基于redis-3.2.4的Redis-Cluster集群搭建 原理 Redis 集群采用了P2P的模式,完全去中心化.Redis 把所有的 Key 分成了 16384 个 slot,每个 Redis ...
- 【pattern】设计模式(1) - 单例模式
前言 好久没写博客,强迫自己写一篇.只是总结一下自己学习的单例模式. 说明 单例模式的定义,摘自baike: 单例模式最初的定义出现于<设计模式>(艾迪生维斯理, 1994):“保证一个类 ...
- 【3】Python中的广播
Python-numpy中有一种很高效的方法:广播. 下面介绍一下广播. 实例:对于这个矩阵,如果想求每列元素的和,怎么才能不用for循环? (1,4)指的是一行四列的矩阵:axis决定了是横向(行 ...
- Java第二节课总结
Java的基本运行单位是类.类由数据成员和函数成员组成.变量的类型之间可以相互转换.String是一个类.static代表静态变量. 运行结果: false false ...
- Codeforces Round #612 (Div. 2) (A-D)
直 接看所有A后面连续P的个数最大值 #include<cstring> #include<cstdio> #include<set> #include<io ...