本文实例讲述了php实现图片添加描边字和马赛克的方法。分享给大家供大家参考。具体实现方法如下:

马赛克:void imagemask ( resource image, int x1, int y1, int x2, int y2, int deep)

imagemask() 把坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)的矩形区域加上马赛克。

deep为模糊程度,数字越大越模糊。

描边: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' 将被加到文件名之后并且会搜索库定义字体路径。

如果指定了字体文件,由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。否则 x,y 定义了第一个字符的右上角。

fontfile 是想要使用的 truetype 字体的文件名。

text 是文本字符串,可以包含 utf-8 字符序列(形式为:{)来访问字体中超过前 255 个的字符。

color 是十六进制的#rrggbb格式的颜色,如#ff0000为红色。

outercolor 描边颜色,十六进制的#rrggbb格式。

/**

* gd image mask

*

* @copyright ugia.cn

*/

function imagemask(&$im, $x1, $y1, $x2, $y2, $deep)

{

for($x = $x1; $x < $x2; $x += $deep)

{

for ($y = $y1; $y < $y2; $y += $deep)

{

$color = imagecolorat ($im, $x + round($deep / 2), $y + round($deep / 2));

imagefilledrectangle ($im, $x, $y, $x + $deep, $y + $deep, $color);

}

}

}

//马赛克用法示例:

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

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

imagemask($im, 57, 22, 103, 40, 8);

imagepng($im);

imagedestroy($im);

?>

运行效果如下图所示:

/**

* 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);

?>

希望本文所述对大家的PHP程序设计有所帮助。

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

  1. 输入框超链接_如何为文字、形状、图片添加超链接?如何为超链接设置颜色?...

    你知道如何为PPT中的文字.形状.图片添加超链接吗?那么,你又知道如何设置超链接的颜色吗?接下来小编就来仔细的讲解一下吧~看图思路更清晰哦! 如何为PPT中的文字.形状.图片添加超链接? 为文字添加超 ...

  2. html超链接图片下 加文字颜色,如何为文字、形状、图片添加超链接?如何为超链接设置颜色?...

    你知道如何为PPT中的文字.形状.图片添加超链接吗?那么,你又知道如何设置超链接的颜色吗?接下来小编就来仔细的讲解一下吧~看图思路更清晰哦! 如何为PPT中的文字.形状.图片添加超链接? 为文字添加超 ...

  3. PHP生成海报 文字描边,海报字体 怎么为海报字体添加描边样式?海报文字描边怎么做?海报字体描边样式制作...

    今天是十月的最后一天啦,转眼2019年只剩下两个月了,这时间啊,走得真快,还没好好感受呢,都快要2020年了.而小编倒好,还觉得现在是2018年呢~哈哈.好啦,不说废话了,还是来看看今天的教程方案吧! ...

  4. 前端生成海报图:html2canvas 生成海报图/网页html转图片

    html2canvas 生成海报图 <html><head> <title>生成海报图Demo</title> </head><bod ...

  5. gif加文字 php,gif动态图片添加文字 gif制作软件 怎样给gif动态图片添加文字

    gif动态图片添加文字 gif制作软件 怎样给gif动态图片添加文字 现在有各种各样的表情包.搞笑图片以及搞笑视频都深受大家的喜爱,之前咱们也有一起学习过怎样用视频制作gif动态图片,相信大家肯定还记 ...

  6. python给批量图片添加文字 脚本,Python实现图片添加文字

    在工作中有时候会给图上添加文字,常用的是PS工具,不过我想通过代码的方式来给图片添加文字. 需要使用的Python的图像库:PIL.更加详细的知识点如下: Imaga模块:用来创建,打开,保存图片文件 ...

  7. vue 生成海报完整代码, (包括远程图片转base64、html2canvas 在IOS系统兼容的解决办法)

    最终效果: 1. html结构分为3部分:最终的海报图 + 内容布局 + 保存提示文字(可忽略) <template><section><img :src="f ...

  8. 网页怎么在图片上添加文字_想给图片添加文字,留白,添加小印章?用手机三步搞定...

    如今越来越多的伙伴喜欢手机摄影,拍好的照片,还可以制作各种精美的图文,直接表达心情的同时,还能获得一片"赞"! 效果1: 图片下方留白+圆形印章 效果2: 图片上下方留白+椭圆形印 ...

  9. python的pillow给图片加文字_Python-Pillow库给图片添加文字、水印

    apt-get install -y python-PIL 在图片右下角添加文字: Python # -*- coding: utf-8 -*- from PIL import Image, Imag ...

最新文章

  1. C语言——把结构体数据封装成TLV格式的数据
  2. 操作系统面试题目详解
  3. 瑞幸咖啡股价再创新低,App 反冲 TOP 1
  4. Alamofire源码解读系列(十二)之请求(Request)
  5. golang | windows平台golang环境搭建(过坑之后)
  6. 数组的存储与初始化、对象数组、数组作为函数参数
  7. 20210330:二叉树力扣相关习题复习(上)
  8. Java多线程系列 JUC线程池01 线程池框架
  9. 分析C#中Dialog控件(转)
  10. 淘宝数据分析工具汇总
  11. Shell编程之常用内置命令
  12. Excel如何锁定首列,教程来啦!怎样将excel的列锁定冻结
  13. 素人程序员:如何在技术浪潮更迭中保持较高成长速度?
  14. 谈谈关于网瘾的看法和建议
  15. css背景图片高斯模糊_如何将网页CSS背景图高斯模糊且全屏显示
  16. 不会用matplotlib画多子图?收好这2个套路
  17. 微信个性签名服务器维护,微信个性签名更改不了系统维护
  18. matlab 电力电子元件对应名称,电力电子与MATLAB应用技术
  19. 前端——IndexedDB
  20. FPGA SEU问题与SEM Core

热门文章

  1. abrtd:Executable ‘some execution‘ doesn‘t belong to any package and ProcessUnpackaged is set to ‘no‘
  2. Python项目实践:霍兰德人格分析雷达图
  3. web前端基础(03html列表知识)
  4. 1075 - Incorrect table definition;there can be only one auto column and it must be defined as a key
  5. netty大白话--字符串的收发(三)
  6. java的设计模式之单列设计模式
  7. tar在linux编译为exe,将Linux代码移植到Windows的简单方法 1
  8. hikvision v2.3控件网页demo_网页禁止复制粘贴怎么解决?教你五招轻松搞定
  9. java 注入日志_java框架学习日志-5(常见的依赖注入)
  10. 项目管理工具project软件学习(八) - 关键路径查询、资源可用性