加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php实现图片添加描边字和马赛克的方法

发布时间:2020-12-13 02:15:54 所属栏目:PHP教程 来源:网络整理
导读:《:php实现图片添加描边字和马赛克的方法》要点: 本文介绍了:php实现图片添加描边字和马赛克的方法,希望对您有用。如果有疑问,可以联系我们。 本篇章节讲解php实现图片添加描边字和马赛克的办法.供大家参考研究.具体实现办法如下: PHP教程 马

《:php实现图片添加描边字和马赛克的方法》要点:
本文介绍了:php实现图片添加描边字和马赛克的方法,希望对您有用。如果有疑问,可以联系我们。

本篇章节讲解php实现图片添加描边字和马赛克的办法.分享给大家供大家参考.具体实现办法如下:PHP教程

马赛克:void imagemask ( resource image,int x1,int y1,int x2,int y2,int deep)PHP教程

imagemask() 把坐标 x1,y1 到 x2,y2(图像左上角为 0,0)的矩形区域加上马赛克.PHP教程

deep为模糊程度,数字越大越模糊.PHP教程

描边:void imagetextouter ( resource image,int size,int x,int y,string color,string fontfile,string text,string outercolor)PHP教程

imagetextouter() 将字符串 text 画到 image 所代表的图像上,从坐标 x,y(左上角为 0,0)开始,颜色为 color,边框所使用的颜色为 outercolor,使用 fontfile 所指定的 truetype 字体文件.PHP教程

如果不指定字体文件,则使用gd的内部字体.根据 php 所使用的 gd 库的不同,如果 fontfile 没有以 ‘/'开头,则 ‘.ttf' 将被加到文件名之后并且会搜索库定义字体路径.PHP教程

如果指定了字体文件,由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角).否则 x,y 定义了第一个字符的右上角.PHP教程

fontfile 是想要使用的 truetype 字体的文件名.PHP教程

text 是文本字符串,可以包含 utf-8 字符序列(形式为:{)来拜访字体中超过前 255 个的字符.PHP教程

color 是十六进制的#rrggbb格式的颜色,如#ff0000为红色.PHP教程

outercolor 描边颜色,十六进制的#rrggbb格式.
PHP教程

代码如下:
<?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实现图片添加描边字和马赛克的方法PHP教程

代码如下:
<?php
/**
?* gd image text outer
?*
?* @copyright ugia.cn
?
?*/
function imagetextouter(&$im,$size,$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,$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);
??? $black = imagecolorallocate($im_tmp,0);
??? $color = imagecolorallocatehex($im,$color);
??? $outer = imagecolorallocatehex($im,$outer);
??? if ($ttf)
??? {
??????? imagettftext($im_tmp,$height - 2,$black,$text);
??????? imagettftext($im,$text);
??????? $y = $y - $height + 2;
??? }
??? else
??? {
??????? imagestring($im_tmp,$black);
??????? imagestring($im,$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,$j - 1) != $white || imagesetpixel($im,$x + $i,$y + $j - 1,$outer);
??????????????? imagecolorat ($im_tmp,$j + 1) != $white || imagesetpixel($im,$y + $j + 1,$i - 1,$j) != $white || imagesetpixel($im,$x + $i - 1,$y + $j,$i + 1,$x + $i + 1,$outer);
??????????????? // 取消注释,与fireworks的发光效果相同
??????????????? /*
??????????????? imagecolorat ($im_tmp,$outer);
??????????????? */
??????????? }
??????? }
??? }
??? imagedestroy($im_tmp);
}

//用法示例:
header("content-type: image/png");
$im = imagecreatefromjpeg("bluesky.jpg");
$white = imagecolorallocate($im,255);
imagetextouter($im,9,10,20,'#000000',"simsun.ttc",'新年快乐','#ffffff');
imagetextouter($im,2,30,'#ffff00',"",'hello,world!','#103993');
imagepng($im);
imagedestroy($im);
?>PHP教程

希望本文所述对大家的PHP程序设计有所赞助.PHP教程

欢迎参与《:php实现图片添加描边字和马赛克的方法》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读