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

由于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. Node.js核心模块-fs文件系统

    fs是file-system的简写,文件系统的意思.在Node中如果想要进行文件操作,就必须引入fs这个核心模块. 引入 const fs = require('fs') fs.readFile(pa ...

  2. nginx tar包安装步骤

    1.将tar包通过 Xftb工具传输到远程服务器 2.通过 cd 命令进入存放nginx包的文件夹目录 3.tar -zxvf + 文件名 解压 4.cd nginx-1.12.0 进入源码目录 5. ...

  3. pycharm-1

    Python 1.4解释器(运行文件) 1.5注释:#单行,ctrl+/ 多行注释       ””” ””” 2.1变量 assert,except:lambda; nonlocal; 2.2数据类 ...

  4. with open()函数中,如何在文件名设置中引用变量(python)

    name = "wangyang" age = " with open("C:/Users/mike1/Desktop/name_age.txt", ...

  5. link(外部资源关系)

    规定了外部资源与当前文档的关系 常于链接样式表<link href="/media/examples/link-element-example.css" rel=" ...

  6. hdu 1087 Super Jumping!(类最长上升子序列)

    题意:在一组数中选取一个上升子序列,使得这个子序列的和最大. 解:和最长上升子序列dp过程相似,设dp[i]为以第i位为结尾最大和,那么dp[i]等于max(dp[0],dp[1],,,,,dp[i- ...

  7. 视频格式转换mp4

    第一步:https://ffmpeg.zeranoe.com/builds/下载ffmpeg 或者:百度云下载: 链接:https://pan.baidu.com/s/1x_QogbV8xFjkYTe ...

  8. Genymotion下载及安装(安卓虚拟机)

    Genymotion下载及安装   一.注册\登录 打开Genymotion官网,https://www.genymotion.com/ ,首先点击右上角的Sign in进行登录操作.如何登录就不细讲 ...

  9. 通过设置iis在局域网中访问网页

    0.准备工作:IIS6.0镜像包,自制的网页文件夹(路径不能是桌面,否则其他电脑将因为没有权限访问系统桌面而不能访问你的网页) 1.进入添加或删除程序,勾上Internet信息服务(IIS),点击下一 ...

  10. C# LINQ学习笔记三:LINQ to OBJECT之操作字符串

    本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5814204.html,记录一下学习过程以备后续查用. 一.统计单词在字符串中出现的次数 请注意,若要执行计数, ...