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 相关文章推荐
第十一节 重载 [11]
Oct 09 PHP
由php的call_user_func传reference引发的思考
Jul 23 PHP
控制PHP的输出:缓存并压缩动态页面
Jun 11 PHP
php调用Google translate_tts api实现代码
Aug 07 PHP
CI框架装载器Loader.php源码分析
Nov 04 PHP
使用PHP similar text计算两个字符串相似度
Nov 06 PHP
Laravel重写用户登录简单示例
Oct 08 PHP
php实现HTML实体编号与非ASCII字符串相互转换类实例
Nov 02 PHP
PHPExcel导出2003和2007的excel文档功能示例
Jan 04 PHP
PHP无限极分类函数的实现方法详解
Apr 15 PHP
php注册系统和使用Xajax即时验证用户名是否被占用
Aug 31 PHP
详解PHP多个进程配合redis的有序集合实现大文件去重
Mar 06 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
Adnroid 微信内置浏览器清除缓存
2016/07/11 PHP
ThinkPHP3.2.3框架邮件发送功能图文实例详解
2019/04/23 PHP
IE6与IE7中,innerHTML获取param的区别
2009/03/15 Javascript
js 对象是否存在判断
2009/07/15 Javascript
js实现图片轮换效果代码
2013/04/16 Javascript
原生js ActiveXObject获取execl里面的值
2013/11/01 Javascript
JavaScript简单实现网页回到顶部功能
2013/11/12 Javascript
Jquery操作radio的简单实例
2014/01/06 Javascript
使用JavaScript和C#中获得referer
2014/11/14 Javascript
关于javascript模块加载技术的一些思考
2014/11/28 Javascript
JavaScript实现当网页加载完成后执行指定函数的方法
2015/03/21 Javascript
js简单实现Select互换数据的方法
2015/08/17 Javascript
微信小程序开发之录音机 音频播放 动画实例 (真机可用)
2016/12/08 Javascript
jQuery.cookie.js实现记录最近浏览过的商品功能示例
2017/01/23 Javascript
vue动态路由实现多级嵌套面包屑的思路与方法
2017/08/16 Javascript
vue组件父子间通信详解(三)
2017/11/07 Javascript
AutoJs实现刷宝短视频的思路详解
2020/05/22 Javascript
关于vue 项目中浏览器跨域的配置问题
2020/11/10 Javascript
把大数据数字口语化(python与js)两种实现
2013/02/21 Python
Python实现从脚本里运行scrapy的方法
2015/04/07 Python
浅析Python中的序列化存储的方法
2015/04/28 Python
Python计时相关操作详解【time,datetime】
2017/05/26 Python
Python处理文本换行符实例代码
2018/02/03 Python
python中count函数简单的实例讲解
2020/02/06 Python
如何基于Python实现数字类型转换
2020/02/07 Python
keras自动编码器实现系列之卷积自动编码器操作
2020/07/03 Python
python 抓取知乎指定回答下视频的方法
2020/07/09 Python
渗透攻击的测试步骤
2014/06/07 面试题
车间调度岗位职责
2013/11/30 职场文书
个人简历自我评价
2014/01/06 职场文书
探亲邀请信范文
2014/01/30 职场文书
设计大赛策划方案
2014/06/13 职场文书
应届毕业生求职信范文
2015/03/19 职场文书
工作态度不好检讨书
2015/05/06 职场文书
简单的辞职信范文(2016最新版)
2015/05/12 职场文书
SQL Server中交叉联接的用法详解
2021/04/22 SQL Server