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来自动调用不同服务器上的flash
Oct 09 PHP
PHP 采集获取指定网址的内容
Jan 05 PHP
php smarty 二级分类代码和模版循环例子
Jun 01 PHP
php number_format() 函数通过千位分组来格式化数字的实现代码
Aug 06 PHP
destoon常用的安全设置概述
Jun 21 PHP
PHP常量使用的几个需要注意的地方(谨慎使用PHP中的常量)
Sep 12 PHP
php生成唯一的订单函数分享
Feb 02 PHP
PHP基于反射机制实现插件的可插拔设计详解
Nov 10 PHP
浅谈PHP匿名函数和闭包
Mar 08 PHP
php和js实现根据子网掩码和ip计算子网功能示例
Nov 09 PHP
PHP+Redis链表解决高并发下商品超卖问题(实现原理及步骤)
Aug 03 PHP
PHP如何通过带尾指针的链表实现'队列'
Oct 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的String类代码
2010/04/20 PHP
php页面消耗内存过大的处理办法
2013/03/18 PHP
PHP实现恶意DDOS攻击避免带宽占用问题方法
2015/05/27 PHP
Zend Framework教程之视图组件Zend_View用法详解
2016/03/05 PHP
利用PHP判断是手机移动端还是PC端访问的函数示例
2017/12/14 PHP
jQuery性能优化技巧分析
2015/02/20 Javascript
JavaScript动态修改网页元素内容的方法
2015/03/21 Javascript
js 将图片连接转换成base64格式的简单实例
2016/08/10 Javascript
Javascript中的getter和setter初识
2017/08/17 Javascript
vue-router启用history模式下的开发及非根目录部署方法
2018/12/23 Javascript
基于js Canvas实现二次贝塞尔曲线
2018/12/25 Javascript
使用VUE实现在table中文字信息超过5个隐藏鼠标移到时弹窗显示全部
2019/09/16 Javascript
[01:36:17]DOTA2-DPC中国联赛 正赛 Ehome vs iG BO3 第一场 1月31日
2021/03/11 DOTA
Python2.5/2.6实用教程 入门基础篇
2009/11/29 Python
Python实现字符串与数组相互转换功能示例
2017/09/22 Python
实例讲解Python脚本成为Windows中运行的exe文件
2019/01/24 Python
python多线程同步实例教程
2019/08/11 Python
python 实现方阵的对角线遍历示例
2019/11/29 Python
如何将你的应用迁移到Python3的三个步骤
2019/12/22 Python
python实现修改固定模式的字符串内容操作示例
2019/12/30 Python
python实现录屏功能(亲测好用)
2020/03/02 Python
CSS3制作ajax loader icon实现思路及代码
2013/08/25 HTML / CSS
使用canvas一步步实现图片打码功能的方法
2019/06/17 HTML / CSS
个人简历自我评价
2014/01/06 职场文书
幼儿园安全检查制度
2014/01/30 职场文书
初中生评语大全
2014/04/24 职场文书
高中英语演讲稿范文
2014/04/24 职场文书
安全责任书范文
2014/08/25 职场文书
纺织工程专业推荐信
2014/09/08 职场文书
2014年党务工作总结
2014/11/25 职场文书
医院党建工作总结2015
2015/05/26 职场文书
学习弘扬焦裕禄精神心得体会
2016/01/23 职场文书
演讲稿之开卷有益
2019/08/07 职场文书
十一月早安语录:把心放轻,人生就是一朵自在的云
2019/11/04 职场文书
MongoDB 常用的crud操作语句
2021/06/20 MongoDB
能让Python提速超40倍的神器Cython详解
2021/06/24 Python