PHP image stroke and mosaic (example)

Source: Internet
Author: User
PHP image stroke and mosaic (example)

  1. /**
  2. * GD image text outer
  3. *
  4. * @ Copyright UGiA. CN
  5. * [Url = home. php? Mod = space & uid = 17823] @ LINK [/url] www.ugia.cn /? P = 88
  6. * @ Edit bbs.it-home.org
  7. */
  8. Function imagetextouter (& $ im, $ size, $ x, $ y, $ color, $ fontfile, $ text, $ outer)
  9. {
  10. If (! Function_exists ('imagecolorallocatehex '))
  11. {
  12. Function ImageColorAllocateHEX ($ im, $ s)
  13. {
  14. If ($ s {0 }== "#") $ s = substr ($ s, 1 );
  15. $ Bg_dec = hexdec ($ s );
  16. Return imagecolorallocate ($ im,
  17. ($ Bg_dec & 0xFF0000)> 16,
  18. ($ Bg_dec & 0x00FF00)> 8,
  19. ($ Bg_dec & 0x0000FF)
  20. );
  21. }
  22. }
  23. $ Ttf = false;
  24. If (is_file ($ fontfile ))
  25. {
  26. $ Ttf = true;
  27. $ Area = imagettfbbox ($ size, $ angle, $ fontfile, $ text );
  28. $ Width = $ area [2]-$ area [0] + 2;
  29. $ Height = $ area [1]-$ area [5] + 2;
  30. }
  31. Else
  32. {
  33. $ Width = strlen ($ text) * 10;
  34. $ Height = 16;
  35. }
  36. $ Im_tmp = imagecreate ($ width, $ height );
  37. $ White = imagecolorallocate ($ im_tmp, 255,255,255 );
  38. $ Black = imagecolorallocate ($ im_tmp, 0, 0, 0 );
  39. $ Color = ImageColorAllocateHEX ($ im, $ color );
  40. $ Outer = ImageColorAllocateHEX ($ im, $ outer );
  41. If ($ ttf)
  42. {
  43. Imagettftext ($ im_tmp, $ size, 0, 0, $ height-2, $ black, $ fontfile, $ text );
  44. Imagettftext ($ im, $ size, 0, $ x, $ y, $ color, $ fontfile, $ text );
  45. $ Y = $ y-$ height + 2;
  46. }
  47. Else
  48. {
  49. Imagestring ($ im_tmp, $ size, 0, 0, $ text, $ black );
  50. Imagestring ($ im, $ size, $ x, $ y, $ text, $ color );
  51. }
  52. For ($ I = 0; $ I <$ width; $ I ++)
  53. {
  54. For ($ j = 0; $ j <$ height; $ j ++)
  55. {
  56. $ C = ImageColorAt ($ im_tmp, $ I, $ j );
  57. If ($ c! ==$ White)
  58. {
  59. ImageColorAt ($ im_tmp, $ I, $ j-1 )! = $ White | imagesetpixel ($ im, $ x + $ I, $ y + $ j-1, $ outer );
  60. ImageColorAt ($ im_tmp, $ I, $ j + 1 )! = $ White | imagesetpixel ($ im, $ x + $ I, $ y + $ j + 1, $ outer );
  61. ImageColorAt ($ im_tmp, $ I-1, $ j )! = $ White | imagesetpixel ($ im, $ x + $ I-1, $ y + $ j, $ outer );
  62. ImageColorAt ($ im_tmp, $ I + 1, $ j )! = $ White | imagesetpixel ($ im, $ x + $ I + 1, $ y + $ j, $ outer );
  63. // Cancel the annotation, which has the same luminous effect as Fireworks.
  64. /*
  65. ImageColorAt ($ im_tmp, $ I-1, $ j-1 )! = $ White | imagesetpixel ($ im, $ x + $ I-1, $ y + $ j-1, $ outer );
  66. ImageColorAt ($ im_tmp, $ I + 1, $ j-1 )! = $ White | imagesetpixel ($ im, $ x + $ I + 1, $ y + $ j-1, $ outer );
  67. ImageColorAt ($ im_tmp, $ I-1, $ j + 1 )! = $ White | imagesetpixel ($ im, $ x + $ I-1, $ y + $ j + 1, $ outer );
  68. ImageColorAt ($ im_tmp, $ I + 1, $ j + 1 )! = $ White | imagesetpixel ($ im, $ x + $ I + 1, $ y + $ j + 1, $ outer );
  69. */
  70. }
  71. }
  72. }
  73. Imagedestroy ($ im_tmp );
  74. }
  75. ?>

2. call example:

  1. Header ("Content-type: image/png ");
  2. $ Im = imagecreatefromjpeg ("bluesky.jpg ");
  3. $ White = imagecolorallocate ($ im, 255,255,255 );
  4. Imagetextouter ($ im, 9, 10, 20, '#000000', "simsun. ttc", 'Happy New Year', '# ffff ');
  5. Imagetextouter ($ im, 2, 10, 30, '# ffff00', "", 'Hello, world! ',' #103993 ');
  6. Imagepng ($ im );
  7. Imagedestroy ($ im );
  8. ?>

Mosaic: void imagemask (resource image, int x1, int y1, int x2, int y2, int deep) imagemask () converts coordinates x1, y1 to x2, add mosaic to the rectangular area of y2 (0, 0 in the upper left corner of the image. Deep indicates the degree of blurring, and the greater the number, the blurrier it.

Effect, such:

1. mosaic function code:

  1. /**
  2. * GD image mask
  3. *
  4. * @ Edit bbs.it-home.org
  5. */
  6. Function imagemask (& $ im, $ x1, $ y1, $ x2, $ y2, $ deep)
  7. {
  8. For ($ x = $ x1; $ x <$ x2; $ x + = $ deep)
  9. {
  10. For ($ y = $ y1; $ y <$ y2; $ y + = $ deep)
  11. {
  12. $ Color = ImageColorAt ($ im, $ x + round ($ deep/2), $ y + round ($ deep/2 ));
  13. Imagefilledrectangle ($ im, $ x, $ y, $ x + $ deep, $ y + $ deep, $ color );
  14. }
  15. }
  16. }
  17. ?>

2. call example:

  1. Header ("Content-type: image/png ");
  2. $ Im = imagecreatefromjpeg ("test.jpg ");
  3. Imagemask ($ im, 57, 22,103, 40, 8 );
  4. Imagepng ($ im );
  5. Imagedestroy ($ im );
  6. ?>

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.