php实现图片添加描边字和马赛克的方法


Posted in PHP onDecember 10, 2014

本文实例讲述了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格式。

<?php 

/** 

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

?>

运行效果如下图所示:

php实现图片添加描边字和马赛克的方法

<?php 

/** 

 * 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 网页过期时间的控制代码
Jun 29 PHP
PHP中英混合字符串截取函数代码
Jul 17 PHP
简单的php写入数据库类代码分享
Jul 26 PHP
解析php入库和出库
Jun 25 PHP
PHP 过滤页面中的BOM(实现代码)
Jun 29 PHP
php使用正则过滤js脚本代码实例
May 10 PHP
php简单判断两个字符串是否相等的方法
Jul 13 PHP
[原创]smarty简单模板变量输出方法
Jul 09 PHP
PHP数据库表操作的封装类及用法实例详解
Jul 12 PHP
PHP+JavaScript实现无刷新上传图片
Feb 21 PHP
PHP代码重构方法漫谈
Apr 17 PHP
PHP中__set()实例用法和基础讲解
Jul 23 PHP
PHP生成条形图的方法
Dec 10 #PHP
php自定文件保存session的方法
Dec 10 #PHP
php通过session防url攻击方法
Dec 10 #PHP
php利用cookies实现购物车的方法
Dec 10 #PHP
php针对cookie操作的队列操作类实例
Dec 10 #PHP
php利用cookie实现自动登录的方法
Dec 10 #PHP
PHP使用header()输出图片缓存实例
Dec 09 #PHP
You might like
在WordPress的文章编辑器中设置默认内容的方法
2015/12/29 PHP
一个JS小玩意 几个属性相加不能超过一个特定值.
2009/09/29 Javascript
ASP Json Parser修正版
2009/12/06 Javascript
自己做的模拟模态对话框实现代码
2012/05/23 Javascript
常见的原始JS选择器使用方法总结
2014/04/09 Javascript
js中的事件捕捉模型与冒泡模型实例分析
2015/01/10 Javascript
jQuery  ready方法实现原理详解
2016/10/19 Javascript
AngularJS双向绑定和依赖反转实例详解
2017/04/15 Javascript
微信小程序之网络请求简单封装实例详解
2017/06/28 Javascript
vue webpack打包后图片路径错误的完美解决方法
2018/12/07 Javascript
vue自定义指令之面板拖拽的实现
2019/04/14 Javascript
如何在微信小程序中存setStorage
2019/12/13 Javascript
使用JS来动态操作css的几种方法
2019/12/18 Javascript
三步搞定:Vue.js调用Android原生操作
2020/09/07 Javascript
利用python写个下载teahour音频的小脚本
2017/05/08 Python
Python实现自动登录百度空间的方法
2017/06/10 Python
Django自定义分页效果
2017/06/27 Python
python多进程实现进程间通信实例
2017/11/24 Python
Python机器学习库scikit-learn安装与基本使用教程
2018/06/25 Python
使用pyqt 实现重复打开多个相同界面
2019/12/13 Python
Python ORM框架Peewee用法详解
2020/04/29 Python
pandas dataframe 中的explode函数用法详解
2020/05/18 Python
Python基于template实现字符串替换
2020/11/27 Python
你对IPv6了解程度
2016/02/09 面试题
Java Servlet的主要功能和作用是什么
2014/02/14 面试题
机械专业应届生求职信
2013/09/21 职场文书
应届生体育教师自荐信
2013/10/03 职场文书
大学专科自荐信
2014/06/17 职场文书
幼儿园八一建军节活动方案
2014/08/27 职场文书
离婚协议书怎么写
2014/09/12 职场文书
大学生军训自我鉴定范文
2014/09/18 职场文书
国防教育标语
2014/10/08 职场文书
村党的群众路线教育实践活动工作总结
2014/10/25 职场文书
婚庆答谢词大全
2015/09/29 职场文书
python中的被动信息搜集
2021/04/29 Python
nginx.conf配置文件结构小结
2022/04/08 Servers