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 相关文章推荐
第十五节--Zend引擎的发展
Nov 16 PHP
php 自写函数代码 获取关键字 去超链接
Feb 08 PHP
网页上facebook分享功能具体实现
Jan 26 PHP
mac环境中使用brew安装php5.5.15
Aug 18 PHP
PHP实现自动登入google play下载app report的方法
Sep 23 PHP
PHP简单生成缩略图相册的方法
Jul 29 PHP
详解Grunt插件之LiveReload实现页面自动刷新(两种方案)
Jul 31 PHP
PHP常见数组函数用法小结
Mar 21 PHP
PHP mysqli事务操作常用方法分析
Jul 22 PHP
php实现QQ小程序发送模板消息功能
Sep 18 PHP
php策略模式简单示例分析【区别于工厂模式】
Sep 25 PHP
php 多进程编程父进程的阻塞与非阻塞实例分析
Feb 22 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
PHP 第一节 php简介
2012/04/28 PHP
ThinkPHP的L方法使用简介
2014/06/18 PHP
php强制用户转向www域名的方法
2015/06/19 PHP
详解Laravel服务容器的绑定与解析
2019/11/05 PHP
javascript 文章截取部分无损html显示实现代码
2010/05/04 Javascript
基于JQuery的抓取博客园首页RSS的代码
2011/12/01 Javascript
nodejs实现获取当前url地址及url各种参数值
2015/06/25 NodeJs
javascript的理解及经典案例分析
2016/05/20 Javascript
JavaScript面向对象分层思维全面解析
2016/11/22 Javascript
javascript学习之json入门
2016/12/22 Javascript
基于jQuery实现数字滚动效果
2017/01/16 Javascript
nodejs读写json文件的简单方法(必看)
2017/03/09 NodeJs
Angular2监听页面大小变化的解决方法
2017/10/09 Javascript
使用Angular CLI生成 Angular 5项目教程详解
2018/03/18 Javascript
vuejs项目打包之后的首屏加载优化及打包之后出现的问题
2018/04/01 Javascript
JS实现的A*寻路算法详解
2018/12/14 Javascript
jquery实现商品sku多属性选择功能(商品详情页)
2019/12/20 jQuery
使用js原生实现年份轮播选择效果实例
2021/01/12 Javascript
[01:01:18]VP vs NIP 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
[38:31]完美世界DOTA2联赛PWL S3 Magma vs GXR 第一场 12.13
2020/12/17 DOTA
Python使用htpasswd实现基本认证授权的例子
2014/06/10 Python
numpy中索引和切片详解
2017/12/15 Python
python中使用zip函数出现错误的原因
2018/09/28 Python
pycharm新建一个python工程步骤
2019/07/16 Python
new_zeros() pytorch版本的转换方式
2020/02/18 Python
python实现梯度下降和逻辑回归
2020/03/24 Python
Pandas的Apply函数具体使用
2020/07/21 Python
TensorFlow2.0使用keras训练模型的实现
2021/02/20 Python
Too Faced官网:美国知名彩妆品牌
2017/03/07 全球购物
高一生物教学反思
2014/01/17 职场文书
母亲追悼会答谢词
2014/01/27 职场文书
毕业生大学生活自我总结
2014/01/31 职场文书
十一酒店活动方案
2014/02/20 职场文书
大学同学聚会感言
2015/07/30 职场文书
上班旷工检讨书
2015/08/15 职场文书
python实现简单的聊天小程序
2021/07/07 Python