项目要求宽,高为传入参数;文字大小,文字间隔需要自动调节;

由于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的更多相关文章

  1. cocos2d-x 利用CCLabelTTF制作文字描边与阴影效果的实现方法

    // // myttf.h// // Created by 王天宇 on 14-6-12. // // #ifndef ____SLG__myttf__ #define ____SLG__myttf_ ...

  2. 利用Adorner制作用于图像裁切的选择框

    原文:利用Adorner制作用于图像裁切的选择框 前天,我写了一篇"使用Adorner显示WPF控件的边界点"的文章.这次,使用从Adorner继承来写一个用于图像裁切的选择框. ...

  3. dreamweaver中的 map怎么调用?_制作热点图像区域

    我们浏览网页时,经常看到一些图片上会出现特别的超链接,即在一张图片上有多个局部区域和不同的网页链接,比如地图链接. 这就是映射图像(Image Map),它是指一幅根据链接对象不同而被人为划分为若干指 ...

  4. HTML5火焰文字特效DEMO演示

    效果展示:http://hovertree.com/texiao/html5/26/ 效果图: 扫描二维码查看效果:

  5. HTML5火焰文字特效DEMO演示---转载

    只有google支持 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  6. PHP中应用GD2函数在图像上添加文字

    <?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/g ...

  7. [2014.01.27]wfTextImage 文字图像组件 1.6

    全新开发的文字转图像组件--wfTextImage,使用简单,功能强大,图像处理效果极佳.     将大段的文本内容转换成GIF图片.     有效防止文字内容被复制抄袭,有效保护文字资料.      ...

  8. 【iOS发展-28】制造业UITabBarController标记控制器、定制UITabBarItem文字图像6途径和More评论

    一个.一个简单的制作过程(实际工程中不建议这样的方式,不要只展示所用原理的理解) 在AppDelegate.m在: - (BOOL)application:(UIApplication *)appli ...

  9. Quart2D文字图像绘制

    上一个是绘制简单图形,这一篇学习绘制文字.图像 //获取画布 CGContextRef context=UIGraphicsGetCurrentContext(); //设置边框颜色 CGContex ...

随机推荐

  1. jQuery XSS漏洞

    漏洞成因: jQuery中过滤用户输入数据所使用的正则表达式存在缺陷,可能导致location.hash跨站脚本攻击. 演示程序: <!DOCTYPE html> <html lan ...

  2. 关于javascript中的内置函数

    (1) parseInt()函数 语法:parseInt(string,[n])   该函数主要将首位为数字的字符串转化为数字,若该字符串不是以数字开头,则返回NaN; n是用于指出字符串中的数据是几 ...

  3. HTML单词

    html超文本标记语言 head 头部font 字体 字形i(italic) 倾斜,斜体字big 大的,字体加大hr 水平线Pre(predefined)预定义h5标题5Div(division)区隔 ...

  4. css字体样式+文本样式

    font-family属性值:具体字体名或者字体集 如果是中文或者有单词之间有空格,需要加双引号 字体集: Serif (有装饰线) Sans-serif (无装饰线) Monospace Cursi ...

  5. kubernetes安装-二进制

    主要参考https://github.com/opsnull/follow-me-install-kubernetes-cluster,采用Flanel和docker 系统信息 角色 系统 CPU C ...

  6. UVA-1588

    只用C来写 题目:https://vjudge.net/problem/UVA-1588 #include<stdio.h> #include<string.h> #defin ...

  7. 如何在CentOS上安装Tensorflow的gpu版本?

    系统配置 系统版本: Centos7.6 语言: Python3.5(anaconda3 4.2) 框架: Tensorflow 安装依赖 sudo yum install openjdk-8-jdk ...

  8. linux 下查看json 文件 使用jq工具

    安装 文档 yum 安装 yum search jq yum -y install jq.x86_64 apt-get install jq jq支持查看 jq . json 文件 查看json文件 ...

  9. es6转码和package.json中的配置

    在线实时转换 需要babel-register .babelrc中: { "presets": [ "es2015" ] } 项目中main.js配置: 前提是 ...

  10. Python循环引用的解决方案

    1.延迟导入:即将 from xxx import yyy 放到函数或类的内部,从而使其作用域变成局部的,但是这样可能会对性能有些影响: 2.将 from xxx import yyy 转换成 imp ...