Home >Backend Development >PHP Tutorial >PHP method to add strokes and mosaics to pictures, _PHP tutorial

PHP method to add strokes and mosaics to pictures, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:041142browse

How to add strokes and mosaic to pictures in php,

The example in this article describes the method of adding strokes and mosaics to pictures in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

Mosaic: void imagemask (resource image, int x1, int y1, int x2, int y2, int deep)

imagemask() adds mosaic to the rectangular area with coordinates x1, y1 to x2, y2 (the upper left corner of the image is 0, 0).

deep is the degree of blur, the larger the number, the blurrier it is.

Stroke: void imagetextouter (resource image, int size, int x, int y, string color, string fontfile, string text, string outercolor)

imagetextouter() draws the string text onto the image represented by image, starting from the coordinates x, y (the upper left corner is 0, 0), the color is color, and the color used for the border is outercolor, specified by fontfile truetype font file.

If no font file is specified, gd's internal font is used. Depending on the gd library used by php, if the fontfile does not start with '/', then '.ttf' will be appended to the file name and the library definition font path will be searched.

If a font file is specified, the coordinates represented by x,y define the base point of the first character (presumably the lower left corner of the character). Otherwise x,y define the upper right corner of the first character.

fontfile is the file name of the truetype font you want to use.

text is a text string that can contain utf-8 character sequences (of the form: {) to access more than the first 255 characters in the font.

color is the color in hexadecimal format #rrggbb, such as #ff0000 is red.

outercolor stroke color, hexadecimal #rrggbb format.

Copy code The code is as follows:
/**
 * 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);
          }
}
}
//Mosaic usage example:
header("content-type: image/png");
$im = imagecreatefromjpeg("test.jpg");
imagemask($im, 57, 22, 103, 40, 8);
imagepng($im);
imagedestroy($im);
?>
The operation effect is as shown below:

Copy code The code is as follows:
/**
 * 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程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/925128.htmlTechArticlephp实现图片添加描边字和马赛克的方法, 本文实例讲述了php实现图片添加描边字和马赛克的方法。分享给大家供大家参考。具体实现方法如...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn