Php implements the method of adding stroke and mosaic to images. _ PHP Tutorial

Source: Internet
Author: User
Php implements the method of adding stroke and mosaic to images ,. Php implements the method of adding stroke and mosaic to images. This article describes how to add stroke and mosaic to images in php. Share it with you for your reference. The specific implementation method is as follows: php implements the method of adding stroke and mosaic to images,

This example describes how to add stroke and mosaic to images in php. Share it with you 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 of the coordinate x1, y1 to x2, y2 (0 in the upper left corner of the image.

Deep indicates the degree of blurring, and the greater the number, the blurrier it.

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

Imagetextouter () draws the string text to the image, starting from the coordinates x, y (0, 0 in the upper left corner). The color is color, and the border color is outercolor, use the truetype font file specified by fontfile.

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

If a font file is specified, the coordinates represented by x and y define the basic points of the first character (probably the lower left corner of the character ). Otherwise, x and y define the upper right corner of the first character.

Fontfile is the file name of the truetype font to be used.

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

Color is a hexadecimal # rrggbb color, for example, # ff0000 is red.

Outercolor stroke color, in hexadecimal # rrggbb format.

The code is as follows:

<? 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 );
}
}
}
// Mosaic Usage example:
Header ("content-type: image/png ");
$ Im = imagecreatefromjpeg ("test.jpg ");
Imagemask ($ im, 57, 22,103, 40, 8 );
Imagepng ($ im );
Imagedestroy ($ im );
?>

Shows the running effect:

The code is 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 );
// Cancel the annotation, which has the same luminous effect 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 example:
Header ("content-type: image/png ");
$ Im = imagecreatefromjpeg ("bluesky.jpg ");
$ White = imagecolorallocate ($ im, 255,255,255 );
Imagetextouter ($ im, 9, 10, 20, '#000000', "simsun. ttc", 'Happy New Year', '# ffff ');
Imagetextouter ($ im, 2, 10, 30, '# ffff00', "", 'Hello, world! ',' #103993 ');
Imagepng ($ im );
Imagedestroy ($ im );
?>

I hope this article will help you with PHP programming.

Examples: This article describes how to add stroke and mosaic to images in php. Share it with you for your reference. The specific implementation method is as follows...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.