参考了讨论的
http://www.thinkphp.cn/topic/52783.html
不过这哥们写的不是很清楚明了 我总结下把代码提交下给大家做个参考
顺便@下管理员我之前那个号啥时候给我解封啊 id:wszxsw
好了废话不多说上代码
composer require topthink/think-image composer 安装好代码以后(说啥不会composer ?左拐~~)
\vendor\topthink\think-image\src 下有个Image.php
在text方法中修改大致的行数为485行
代码整体如下:可以直接复制
public function text($text, $font, $size, $color = '#00000000',
$locate = self::WATER_SOUTHEAST, $offset = 0, $angle = 0) {
$text = explode('|', $text); //把字符串文字转为数组,没有分隔符的文字可以通过函数按照制定长度来分割
$maxtext = 0;
foreach ($text as $val) {
$maxtext = strlen($val) > strlen($maxtext) ? $val : $maxtext;
}
if (!is_file($font)) {
throw new ImageException("不存在的字体文件:{$font}");
}
//获取文字信息
//$info = imagettfbbox($size, $angle, $font, $text);
$info = imagettfbbox($size, $angle, $font, $maxtext);
$textHeight = $info[1] - $info[7];
$lineHeight = $textHeight + 3;
$minx = min($info[0], $info[2], $info[4], $info[6]);
$maxx = max($info[0], $info[2], $info[4], $info[6]);
$miny = min($info[1], $info[3], $info[5], $info[7]);
$maxy = max($info[1], $info[3], $info[5], $info[7]);
/* 计算文字初始坐标和尺寸 */
$x = $minx;
$y = abs($miny);
$w = $maxx - $minx;
$h = $maxy - $miny;
/* 设定文字位置 */
switch ($locate) {
/* 右下角文字 */
case self::WATER_SOUTHEAST:
$x += $this->info['width'] - $w;
$y += $this->info['height'] - $h;
break;
/* 左下角文字 */
case self::WATER_SOUTHWEST:
$y += $this->info['height'] - $h;
break;
/* 左上角文字 */
case self::WATER_NORTHWEST:
// 起始坐标即为左上角坐标,无需调整
break;
/* 右上角文字 */
case self::WATER_NORTHEAST:
$x += $this->info['width'] - $w;
break;
/* 居中文字 */
case self::WATER_CENTER:
$x += ($this->info['width'] - $w) / 2;
$y += ($this->info['height'] - $h) / 2;
break;
/* 下居中文字 */
case self::WATER_SOUTH:
$x += ($this->info['width'] - $w) / 2;
$y += $this->info['height'] - $h;
break;
/* 右居中文字 */
case self::WATER_EAST:
$x += $this->info['width'] - $w;
$y += ($this->info['height'] - $h) / 2;
break;
/* 上居中文字 */
case self::WATER_NORTH:
$x += ($this->info['width'] - $w) / 2;
break;
/* 左居中文字 */
case self::WATER_WEST:
$y += ($this->info['height'] - $h) / 2;
break;
default:
/* 自定义文字坐标 */
if (is_array($locate)) {
list($posx, $posy) = $locate;
$x += $posx;
$y += $posy;
} else {
throw new ImageException('不支持的文字位置类型');
}
}
/* 设置偏移量 */
if (is_array($offset)) {
$offset = array_map('intval', $offset);
list($ox, $oy) = $offset;
} else {
$offset = intval($offset);
$ox = $oy = $offset;
}
/* 设置颜色 */
if (is_string($color) && 0 === strpos($color, '#')) {
$color = str_split(substr($color, 1), 2);
$color = array_map('hexdec', $color);
if (empty($color[3]) || $color[3] > 127) {
$color[3] = 0;
}
} elseif (!is_array($color)) {
throw new ImageException('错误的颜色值');
}
do {
/* 写入文字 */
$col = imagecolorallocatealpha($this->im, $color[0], $color[1], $color[2], $color[3]);
foreach ($text as $key => $val) {
imagettftext($this->im, $size, $angle, $x + $ox, $y + $oy + $key * $lineHeight, $col, $font, $val);
}
} while (!empty($this->gif) && $this->gifNext());
return $this;
}
为了方便大家知道是修改的哪行代码顺便标识下修改的信息 使用GIT或者svn的用户对这个界面就很清楚了。(我是不是个暖男)

使用如下
public function index()
{
$image = \think\Image::open('./123.jpg');
$a = rand(1,999);
// 给原图左上角添加水印并保存water_image.png
$ss = $image
->text('刘|玉|尧','simkai.ttf',20,'#ffffff',\Think\Image::WATER_CENTER,0,0)
->save($a.'text_image.png');
echo "<img src='/".$a."text_image.png'/>";
}
}

最佳答案
