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

由于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();

最新文章

  1. Tomcat指定的服务已存在
  2. varchar(n),nvarchar(n) 长度、性能、及所占空间的说明
  3. babylonjs
  4. mysqli和mysql和pdo查询
  5. input text输完自动跳到下一个
  6. AFNnetworking快速教程,官方入门教程译
  7. hdu 4614 Vases and Flowers (二分 线段树)
  8. android Gallery滑动不流畅的解决
  9. orcl 删除重复的行
  10. leetcode 名单 Insertion Sort List
  11. lintcode.67 二叉树中序遍历
  12. HTML表单属性集合
  13. ARP协议与ARP攻击入门
  14. Python基础之面对对象进阶
  15. Nginx 请求的11个阶段
  16. lnmp 基础设置
  17. js获取图片的原始尺寸
  18. vue图片上传及java存储图片(亲测可用)
  19. ansible debug模块学习笔记
  20. JS对象转URL参数(原生JS和jQuery两种方式)

热门文章

  1. Gird(2)
  2. java使用原生MySQL实现数据的增删改查以及数据库连接池技术
  3. 如何将旧Mac的数据迁移到新的MacBook Pro?
  4. Android注册时验证码的生产方法
  5. L001.PyQt
  6. C#设计模式学习笔记:设计原则
  7. 76.0.3809.100版本的谷歌浏览器对应能用的chromedriver版本
  8. 《C++面向对象程序设计》第6章课后编程题2拓展
  9. PAT (Basic Level) Practice (中文)1048 数字加密 (20 分)
  10. Wannafly Camp 2020 Day 3D 求和 - 莫比乌斯反演,整除分块,STL,杜教筛