This article describes the PHP implementation of the image add stroke word and mosaic method. Share to everyone for your reference. The implementation methods are as follows:
Mosaic: void Imagemask (resource image, int x1, int y1, int x2, int y2, int deep)
Imagemask () x1,y1 the coordinates to the X2,Y2 (the upper-left corner of the image is 0, 0) with a mosaic in the rectangular area.
Deep is blurred, the larger the number the more blurred.
Strokes: void Imagetextouter (resource image, int size, int x, int y, string color, string fontfile, string text, String Outerc Olor)
Imagetextouter () draws the text of the string to the image represented by image, starting with the coordinates x,y (0, 0) in the upper-left corner, the color, the color of the border, the Outercolor, using the TrueType specified by Fontfile The font file.
If you do not specify a font file, the internal font for GD is used. Depending on the GD library used by PHP, if Fontfile does not start with '/', then '. TTF ' will be added to the filename and the library will be searched for the font path.
If a font file is specified, the coordinates represented by X,y define the basic point of the first character (presumably the lower-left corner of the character). Otherwise, the x,y defines the upper-right corner of the first character.
Fontfile is the file name of the TrueType font you want to use.
Text is a literal string that can contain a sequence of utf-8 characters (in the form: {) to access more than the first 255 characters in the font.
Color is the hexadecimal #rrggbb format of the colors, such as #ff0000 red.
Outercolor stroke color, hexadecimal #rrggbb format.
Copy Code code as follows:
<?php
/**
* GD Image Mask
*
* @copyright ugia.cn
*/
Function Imagemask (& $im, $x 1, $y 1, $x 2, $y 2, $deep)
{
for ($x = $x 1; $x < $x 2; $x + = $deep)
{
for ($y = $y 1; $y < $y 2; $y + = $deep)
{
$color = Imagecolorat ($im, $x + round ($deep/2), $y + round ($deep/2));
Imagefilledrectangle ($im, $x, $y, $x + $deep, $y + $deep, $color);
}
}
}
Examples of mosaic usage:
Header ("Content-type:image/png");
$im = Imagecreatefromjpeg ("test.jpg");
Imagemask ($im, 57, 22, 103, 40, 8);
Imagepng ($im);
Imagedestroy ($im);
?>
The effect is as shown in the following illustration:
Copy Code code as follows:
<?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);
Uncomment the same glow as 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);
}
Usage examples:
Header ("Content-type:image/png");
$im = Imagecreatefromjpeg ("bluesky.jpg");
$white = Imagecolorallocate ($im, 255,255,255);
Imagetextouter ($im, 9, A, ' #000000 ', ' SIMSUN.TTC ', ' Happy New Year ', ' #ffffff ');
Imagetextouter ($im, 2, A, ' #ffff00 ', ' ", ' Hello, world! ', ' #103993 ');
Imagepng ($im);
Imagedestroy ($im);
?>
I hope this article will help you with your PHP program design.