教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP图片描边字与马赛克(示例)

PHP图片描边字与马赛克(示例)

发布时间:2017-08-12   编辑:jiaochengji.com
本文介绍下,在php代码中为图片添加描边字与马赛克的办法,学习下php GD库函数imagetextouter、imagemask的用法,有需要的朋友参考下。

在php中用GD给图片添加文字时,由于图片的颜色不是单一的,导致写上去的文字在图片颜色跟文字颜色相近的地方就比较模糊。

以下分享二个小函数,一个是实现所谓的“描边字”,另一个是将某一图片的指定区域变成马赛克。

描边: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图片描边字

1,描边函数代码:
 

复制代码 代码示例:
<?php
/**
 * GD image text outer
 *
 * @copyright UGiA.CN
 * @link www.ugia.cn/?p=88
 * @edit www.jbxue.com
 */
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);
}
?>

2,调用示例:
 

复制代码 代码示例:
<?php
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);
?>

再来说马赛克:void imagemask ( resource image, int x1, int y1, int x2, int y2, int deep)
imagemask() 把坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)的矩形区域加上马赛克。
deep为模糊程度,数字越大越模糊。

效果,如下图:
 

php图片加马赛克

1,马赛克函数代码:
 

复制代码 代码示例:
<?php
/**
 * GD image mask
 *
 * @edit www.jbxue.com
 */
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);
        }
    }
}
?>

2,调用示例:
 

复制代码 代码示例:
<?php
header("Content-type: image/png");
$im = imagecreatefromjpeg("test.jpg");
imagemask($im, 57, 22, 103, 40, 8);
imagepng($im);
imagedestroy($im);
?>

您可能感兴趣的文章:
PHP图片描边字与马赛克(示例)
Photoshop在图片上打马赛克方法分享
photoshop合成疯狂的摩托车比赛海报教程
如何使用HTML5 canvas实现图像的马赛克
python能做什么有趣的东西
如何使用canvas实现图片马赛克
关于canvas下载二维码和图片加水印的方法
HTML5 canvas如何实现马赛克的淡入淡出效果(代码)
photoshop设计赛车主题宣传海报合成教程
分享8个PHP开发常用代码片段

关键词: 马赛克   
[关闭]
~ ~