PHP class implementing Chinese circular seals

Source: Internet
Author: User
PHP class implementing Chinese circular seals

  1. /*
  2. * Chinese circular seals
  3. * @ Author lkk/lianq.net
  4. * @ Create on
  5. * @ Example:
  6. * $ Seal = new circleSeal ('You and I are standing in the Middle East, West, North and South ', 40 );
  7. * $ Seal-> doImg ();
  8. */
  9. Class circleSeal {
  10. Private $ sealString; // seal character
  11. Private $ strMaxLeng; // maximum character length
  12. Private $ sealRadius; // seal radius
  13. Private $ rimWidth; // border thickness
  14. Private $ innerRadius; // inner radius
  15. Private $ startRadius; // star radius
  16. Private $ startAngle; // The angle of the pentagram tilt.
  17. Private $ backGround; // seal color
  18. Private $ centerDot; // center coordinate
  19. Private $ img; // graphical resource handle
  20. Private $ font; // The specified font.
  21. Private $ fontSize; // specify the font size
  22. Private $ width; // image width
  23. Private $ height; // Image height
  24. Private $ points; // coordinates of each vertex of the pentagram
  25. Private $ charRadius; // string radius
  26. Private $ charAngle; // string skew angle
  27. Private $ spacing; // character interval angle
  28. // Constructor
  29. Public function _ construct ($ str = '', $ rad = 75, $ rmwidth = 6, $ strad = 24, $ stang = 0, $ crang = 0, $ fsize = 16, $ inrad = 0 ){
  30. $ This-> sealString = empty ($ str )? 'Seal test String': $ str;
  31. $ This-> strMaxLeng = 12;
  32. $ This-> sealRadius = $ rad;
  33. $ This-> rimWidth = $ rmwidth;
  34. $ This-> startRadius = $ strad;
  35. $ This-> startAngle = $ stang;
  36. $ This-> charAngle = $ crang;
  37. $ This-> centerDot = array ('x' => $ rad, 'y' => $ rad );
  38. $ This-> font = dirname (_ FILE _). '/simkai. ttf ';
  39. $ This-> fontSize = $ fsize;
  40. $ This-> innerRadius = $ inrad; // The default value is 0. no
  41. $ This-> spacing = 1;
  42. }
  43. // Create image resources
  44. Private function createImg (){
  45. $ This-> width = 2 * $ this-> sealRadius;
  46. $ This-> height = 2 * $ this-> sealRadius;
  47. $ This-> img = imagecreate ($ this-> width, $ this-> height );
  48. Imagecolorresolvealpha ($ this-> img, 255,255,255,127 );
  49. $ This-> backGround = imagecolorallocate ($ this-> img, 255, 0 );
  50. }
  51. // Draw a seal border
  52. Private function drawRim (){
  53. For ($ I = 0; $ I <$ this-> rimWidth; $ I ++ ){
  54. Imagearc ($ this-> img, $ this-> centerDot ['x'], $ this-> centerDot ['Y'], $ this-> width-$ I, $ this-> height-$ I, 0,360, $ this-> backGround );
  55. }
  56. }
  57. // Draw incircle
  58. Private function drawInnerCircle (){
  59. Imagearc ($ this-> img, $ this-> centerDot ['x'], $ this-> centerDot ['Y'], 2 * $ this-> innerRadius, 2 x $ this-> innerRadius, 0,360, $ this-> backGround );
  60. }
  61. // Draw a string
  62. Private function drawString (){
  63. // Encoding
  64. $ Charset = mb_detect_encoding ($ this-> sealString );
  65. If ($ charset! = 'Utf-8 '){
  66. $ This-> sealString = mb_convert_encoding ($ this-> sealString, 'utf-8', 'gbk ');
  67. }
  68. // Metering
  69. $ This-> charRadius = $ this-> sealRadius-$ this-> rimWidth-$ this-> fontSize; // string radius
  70. $ Leng = mb_strlen ($ this-> sealString, 'utf8'); // string length
  71. If ($ leng> $ this-> strMaxLeng) $ leng = $ this-> strMaxLeng;
  72. $ AvgAngle = 360/($ this-> strMaxLeng); // average character skew
  73. // Split and write strings
  74. $ Words = array (); // character array
  75. For ($ I = 0; $ I <$ leng; $ I ++ ){
  76. $ Words [] = mb_substr ($ this-> sealString, $ I, 1, 'utf8 ');
  77. $ R = 630 + $ this-> charAngle + $ avgAngle * ($ I-$ leng/2) + $ this-> spacing * ($ i-1); // coordinate angle
  78. $ R = 720-$ this-> charAngle + $ avgAngle * ($ leng-2 * $ i-1)/2 + $ this-> spacing * (1-$ I ); // character angle
  79. $ X = $ this-> centerDot ['x'] + $ this-> charRadius * cos (deg 2rad ($ r); // x coordinate of the character
  80. $ Y = $ this-> centerDot ['Y'] + $ this-> charRadius * sin (deg 2rad ($ r); // y coordinate of the character
  81. Imagettftext ($ this-> img, $ this-> fontSize, $ R, $ x, $ y, $ this-> backGround, $ this-> font, $ words [$ I]);
  82. }
  83. }
  84. // Draw a star
  85. Private function drawStart (){
  86. $ Ang_out = 18 + $ this-> startAngle;
  87. $ Ang_in = 56 + $ this-> startAngle;
  88. $ Rad_out = $ this-> startRadius;
  89. $ Rad_in = $ rad_out * 0.382;
  90. For ($ I = 0; $ I <5; $ I ++ ){
  91. // Five vertex coordinates
  92. $ This-> points [] = $ rad_out * cos (2 * M_PI/5 * $ I-deg 2rad ($ ang_out )) + $ this-> centerDot ['x'];
  93. $ This-> points [] = $ rad_out * sin (2 * M_PI/5 * $ I-deg 2rad ($ ang_out )) + $ this-> centerDot ['Y'];
  94. // Inner concave coordinate
  95. $ This-> points [] = $ rad_in * cos (2 * M_PI/5 * ($ I + 1)-deg 2rad ($ ang_in )) + $ this-> centerDot ['x'];
  96. $ This-> points [] = $ rad_in * sin (2 * M_PI/5 * ($ I + 1)-deg 2rad ($ ang_in )) + $ this-> centerDot ['Y'];
  97. }
  98. Imagefilledpolygon ($ this-> img, $ this-> points, 10, $ this-> backGround );
  99. }
  100. // Output
  101. Private function outPut (){
  102. Header ('content-type: image/png ');
  103. Imagepng ($ this-> img );
  104. Imagedestroy ($ this-> img );
  105. }
  106. // Generate externally
  107. Public function doImg (){
  108. $ This-> createImg ();
  109. $ This-> drawRim ();
  110. $ This-> drawInnerCircle ();
  111. $ This-> drawString ();
  112. $ This-> drawStart ();
  113. $ This-> outPut ();
  114. }
  115. }


PHP

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.