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_Flame(Version:Progress)的原代码
Oct 09 PHP
PHP 采集程序原理分析篇
Mar 05 PHP
mac下使用brew配置环境的步骤分享
May 23 PHP
php小技巧 把数组的键和值交换形成了新的数组,查找值取得键
Jun 02 PHP
浅析十款PHP开发框架的对比
Jul 05 PHP
php 表单提交大量数据发生丢失的解决方法
Mar 03 PHP
Yii 快速,安全,专业的PHP框架
Sep 03 PHP
PHP生成静态HTML页面最简单方法示例
Apr 09 PHP
10个对初学者非常有用的PHP技巧
Apr 06 PHP
总结PHP如何获取当前主机、域名、网址、路径、端口和参数等
Sep 09 PHP
thinkPHP批量删除的实现方法分析
Nov 09 PHP
php图片裁剪函数
Oct 31 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
在任意字符集下正常显示网页的方法一
2007/04/01 PHP
PHP学习笔记之一
2011/01/17 PHP
理解和运用PHP中的多态性[译]
2011/08/02 PHP
[原创]php实现子字符串位置相互对调互换的方法
2016/06/02 PHP
thinkphp自带验证码全面解析
2016/09/18 PHP
PHP微信红包生成代码分享
2016/10/06 PHP
使用Zttp简化Guzzle 调用
2017/07/02 PHP
php 使用 __call实现重载功能示例
2019/11/18 PHP
非主流的textarea自增长实现js代码
2011/12/20 Javascript
JS代码判断IE6,IE7,IE8,IE9的函数代码
2013/08/02 Javascript
获取3个数组不重复的值的具体实现
2013/12/30 Javascript
Jquery动态替换div内容及动态展示的方法
2015/01/23 Javascript
Angularjs中如何使用filterFilter函数过滤
2016/02/06 Javascript
jquery.zclip轻量级复制失效问题
2017/01/08 Javascript
原生JavaScript实现Tooltip浮动提示框特效
2017/03/07 Javascript
nodejs socket实现的服务端和客户端功能示例
2017/06/02 NodeJs
JavaScript高阶函数_动力节点Java学院整理
2017/06/28 Javascript
vue实现评价星星功能
2020/06/30 Javascript
[48:38]DOTA2亚洲邀请赛 3.31 小组赛 B组 Mineski vs Secret
2018/03/31 DOTA
使用python写一个自动浏览文章的脚本实例
2019/12/05 Python
解决pytorch报错:AssertionError: Invalid device id的问题
2020/01/10 Python
python实现Pyecharts实现动态地图(Map、Geo)
2020/03/25 Python
Python Tornado实现WEB服务器Socket服务器共存并实现交互的方法
2020/05/26 Python
EJB2和EJB3在架构上的不同点
2014/09/29 面试题
Exception类的常用方法
2012/06/16 面试题
幼儿园元旦活动感言
2014/03/02 职场文书
竞选大队长演讲稿
2014/04/29 职场文书
关于随地扔垃圾的检讨书
2014/09/30 职场文书
物价局领导班子四风问题整改措施
2014/10/26 职场文书
中秋节慰问信
2015/02/15 职场文书
通知的写法
2015/04/23 职场文书
个人维稳承诺书
2015/05/04 职场文书
《鲁班学艺》读后感3篇
2019/11/27 职场文书
CSS3 制作的悬停缩放特效
2021/04/13 HTML / CSS
SpringBoot 集成Redis 过程
2021/06/02 Redis
Python可视化神器pyecharts之绘制箱形图
2022/07/07 Python