PHP生成海报 文字描边,php实现图片添加描边字和马赛克的方法

这篇博客介绍了如何在PHP中利用GD库创建带描边效果的文本输出。`imagetextouter()`函数用于在图像上绘制文字,支持设置字体大小、位置、颜色及描边颜色,并能处理TrueType字体文件。通过示例代码展示了如何在图片上添加多行不同颜色和描边效果的文字,最后输出PNG图像。
摘要由CSDN通过智能技术生成

描边:void imagetextouter ( resource image, int size, int x, int y, string color, string fontfile, string text, string outercolor)

imagetextouter() 将字符串 text 画到 image 所代表的图像上,从坐标 x,y(左上角为 0, 0)开始,颜色为 color,边框所使用的颜色为 outercolor,使用 fontfile 所指定的 truetype 字体文件。

如果不指定字体文件,则使用gd的内部字体。根据 php 所使用的 gd 库的不同,如果 fontfile 没有以 ‘/'开头,则 ‘.ttf' 将被加到文件名之后并且会搜索库定义字体路径。

/**

* gd image text outer

*

* @copyright ugia.cn

*/

function imagetextouter(&$im, $size, $x, $y, $color, $fontfile, $text, $outer)

{

if (!function_exists('imagecolorallocatehex'))

{

function imagecolorallocatehex($im, $s)

{

if($s{0} == "#") $s = substr($s,1);

$bg_dec = hexdec($s);

return imagecolorallocate($im,

($bg_dec & 0xff0000) >> 16,

($bg_dec & 0x00ff00) >>  8,

($bg_dec & 0x0000ff)

);

}

}

$ttf = false;

if (is_file($fontfile))

{

$ttf = true;

$area = imagettfbbox($size, $angle, $fontfile, $text);

$width  = $area[2] - $area[0] + 2;

$height = $area[1] - $area[5] + 2;

}

else

{

$width  = strlen($text) * 10;

$height = 16;

}

$im_tmp = imagecreate($width, $height);

$white = imagecolorallocate($im_tmp, 255, 255, 255);

$black = imagecolorallocate($im_tmp, 0, 0, 0);

$color = imagecolorallocatehex($im, $color);

$outer = imagecolorallocatehex($im, $outer);

if ($ttf)

{

imagettftext($im_tmp, $size, 0, 0, $height - 2, $black, $fontfile, $text);

imagettftext($im, $size, 0, $x, $y, $color, $fontfile, $text);

$y = $y - $height + 2;

}

else

{

imagestring($im_tmp, $size, 0, 0, $text, $black);

imagestring($im, $size, $x, $y, $text, $color);

}

for ($i = 0; $i < $width; $i ++)

{

for ($j = 0; $j < $height; $j ++)

{

$c = imagecolorat($im_tmp, $i, $j);

if ($c !== $white)

{

imagecolorat ($im_tmp, $i, $j - 1) != $white || imagesetpixel($im, $x + $i, $y + $j - 1, $outer);

imagecolorat ($im_tmp, $i, $j + 1) != $white || imagesetpixel($im, $x + $i, $y + $j + 1, $outer);

imagecolorat ($im_tmp, $i - 1, $j) != $white || imagesetpixel($im, $x + $i - 1, $y + $j, $outer);

imagecolorat ($im_tmp, $i + 1, $j) != $white || imagesetpixel($im, $x + $i + 1, $y + $j, $outer);

// 取消注释,与fireworks的发光效果相同

/*

imagecolorat ($im_tmp, $i - 1, $j - 1) != $white || imagesetpixel($im, $x + $i - 1, $y + $j - 1, $outer);

imagecolorat ($im_tmp, $i + 1, $j - 1) != $white || imagesetpixel($im, $x + $i + 1, $y + $j - 1, $outer);

imagecolorat ($im_tmp, $i - 1, $j + 1) != $white || imagesetpixel($im, $x + $i - 1, $y + $j + 1, $outer);

imagecolorat ($im_tmp, $i + 1, $j + 1) != $white || imagesetpixel($im, $x + $i + 1, $y + $j + 1, $outer);

*/

}

}

}

imagedestroy($im_tmp);

}

//用法示例:

header("content-type: image/png");

$im = imagecreatefromjpeg("bluesky.jpg");

$white = imagecolorallocate($im, 255,255,255);

imagetextouter($im, 9, 10, 20, '#000000', "simsun.ttc", '新年快乐', '#ffffff');

imagetextouter($im, 2, 10, 30, '#ffff00', "", 'hello, world!' , '#103993');

imagepng($im);

imagedestroy($im);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值