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 相关文章推荐
Mysql的GROUP_CONCAT()函数使用方法
Mar 28 PHP
用PHP实现的四则运算表达式计算实现代码
Aug 02 PHP
关于Iframe如何跨域访问Cookie和Session的解决方法
Apr 15 PHP
php禁止浏览器使用缓存页面的方法
Nov 07 PHP
php5.4以下版本json不支持不转义内容中文的解决方法
Jan 13 PHP
PHP自定义函数实现格式化秒的方法
Sep 14 PHP
PHP多维数组元素操作类的方法
Nov 14 PHP
php mysql实现mysql_select_db选择数据库
Dec 30 PHP
PHP实现的杨辉三角求解算法分析
Mar 11 PHP
PHP中ltrim()函数的用法与实例讲解
Mar 28 PHP
在Laravel的Model层做数据缓存的实现
Sep 26 PHP
PHP高并发和大流量解决方案整理
Dec 24 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实现多图片上传类实例
2014/07/26 PHP
PHP内存缓存Memcached类实例
2014/12/08 PHP
php实现图片按比例截取的方法
2017/02/06 PHP
php file_get_contents取文件中数组元素的方法
2017/04/01 PHP
PHP学习记录之数组函数
2018/06/01 PHP
浅谈thinkphp的nginx配置,以及重写隐藏index.php入口文件方法
2019/10/12 PHP
解决extjs在firefox中关闭窗口再打开后iframe中js函数访问不到的问题
2008/11/06 Javascript
一个CSS+jQuery实现的放大缩小动画效果
2014/02/19 Javascript
javascript初学者常用技巧
2014/09/02 Javascript
JavaScript分页功能的实现方法
2015/04/25 Javascript
Bootstrap的popover(弹出框)在append后弹不出(失效)
2017/02/27 Javascript
axios发送post请求springMVC接收不到参数的解决方法
2018/03/05 Javascript
Webpack中雪碧图插件使用详解
2018/05/25 Javascript
Express之托管静态文件的方法
2018/06/01 Javascript
Vue的Eslint配置文件eslintrc.js说明与规则介绍
2020/02/03 Javascript
JavaScript观察者模式原理与用法实例详解
2020/03/10 Javascript
JavaScript实现网页计算器功能
2020/10/29 Javascript
基于Vue2实现移动端图片上传、压缩、拖拽排序、拖拽删除功能
2021/01/05 Vue.js
python中的多线程实例教程
2014/08/27 Python
python实现上传样本到virustotal并查询扫描信息的方法
2014/10/05 Python
两个使用Python脚本操作文件的小示例分享
2015/08/27 Python
Python 多线程抓取图片效率对比
2016/02/27 Python
关于numpy中np.nonzero()函数用法的详解
2017/02/07 Python
Windows下PyCharm安装图文教程
2018/08/27 Python
python读取TXT每行,并存到LIST中的方法
2018/10/26 Python
python爬虫 基于requests模块发起ajax的get请求实现解析
2019/08/20 Python
Django REST framwork的权限验证实例
2020/04/02 Python
python实现三种随机请求头方式
2021/01/05 Python
夜大自我鉴定
2013/10/31 职场文书
前厅部经理岗位职责范文
2014/02/04 职场文书
颐和园导游词
2015/01/30 职场文书
工作时间调整通知
2015/04/24 职场文书
考生诚信考试承诺书
2015/04/29 职场文书
电影建国大业观后感
2015/06/01 职场文书
2016机关干部作风建设心得体会
2016/01/21 职场文书
TV动画《间谍过家家》公开PV
2022/03/20 日漫