Add strokes and mosaic php code to the picture

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags code example file function image network network programming php

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);
}
}
}
?>


Example:
<? php
header ("content-type: image / png");
$ im = imagecreatefromjpeg ("test.jpg");
imagemask ($ im, 57, 22, 103, 40, 8);
imagepng ($ im);
imagedestroy ($ im);
?>


<? 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 comment, and fireworks glow the same effect
/ *
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);
}
?>


Example:
<? php
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', '#ffffff');
imagetextouter ($ im, 2, 10, 30, '# ffff00', "", 'hello, world!', '# 103993');
imagepng ($ im);
imagedestroy ($ im);
?>


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

imagemask () Mosaic a rectangle of coordinates x1, y1 through x2, y2 (0, 0 at the top left of the image).

Deep for the degree of ambiguity, the greater the number of the more fuzzy.
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 represented by the image, starting with the coordinates x, y (0, 0 at the top left), the color is color, and the color of the border is outercolor, using the truetype font specified by fontfile file.

If you do not specify a font file, use gd's internal font. Depending on the gd library used by php, if fontfile does not begin with '/', '.ttf' will be added after the filename and will search the library for the defined font path.

If you specify a font file, the coordinates represented by x, y define the base point of the first character (roughly the lower-left corner of the character). Otherwise x, y defines the upper right corner of the first character.

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

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

color is the color of the #rrggbb format in hexadecimal, for example #ff0000 is red.

outercolor stroke color, hex #rrggbb format.

Related Article

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.