PHP class for implementing a Chinese round seal

Source: Internet
Author: User
Tags cos sin
  1. /*
  2. * Chinese round seal type
  3. * @author lkk/lianq.net
  4. * @create on 10:03 2012-5-29
  5. * @example:
  6. * $seal = new Circleseal (' You and me he sits East ', 75,6,24,0,0,16,40);
  7. * $seal->doimg ();
  8. */
  9. Class Circleseal {
  10. Private $sealString; Stamp character
  11. Private $strMaxLeng; Maximum characters length
  12. Private $sealRadius; Seal radius
  13. Private $rimWidth; Border thickness
  14. Private $innerRadius; Radius of Inner Circle
  15. Private $startRadius; Pentagram radius
  16. Private $startAngle; Pentagram Tilt Angle
  17. Private $backGround; Seal color
  18. Private $centerDot; Center coordinates
  19. Private $img; Graphics resource Handle
  20. Private $font; The specified font
  21. Private $fontSize; Specify font size
  22. Private $width; Picture width
  23. Private $height; Picture height
  24. Private $points; Point coordinates of the pentagram
  25. Private $charRadius; String radius
  26. Private $charAngle; String Tilt angle
  27. Private $spacing; Character Interval angle
  28. Construction method
  29. Public function __construct ($str = ", $rad = $rmwidth = 6, $strad =, $stang = 0, $crang = 0, $fsize = +, $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; Default 0, no
  41. $this->spacing = 1;
  42. }
  43. Create a picture resource
  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,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 Inner Circle
  58. Private Function drawinnercircle () {
  59. Imagearc ($this->img, $this->centerdot[' x '], $this->centerdot[' y '],2* $this->innerradius,2* $this innerradius,0,360, $this->background);
  60. }
  61. Draw a String
  62. Private Function drawstring () {
  63. Encoding processing
  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. Related 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 =/($this->strmaxleng); Average character tilt
  73. Splitting and writing 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 (Deg2rad ($r)); The x-coordinate of the character
  80. $y = $this->centerdot[' y ') + $this->charradius * sin (Deg2rad ($r)); The 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 Pentagram
  85. Private Function Drawstart () {
  86. $ang _out = + $this->startangle;
  87. $ang _in = + $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-deg2rad ($ang _out)) + $this->centerdot[' x '];
  93. $this->points[] = $rad _out * sin (2*m_pi/5* $i-deg2rad ($ang _out)) + $this->centerdot[' y '];
  94. The point coordinates of the inner concave
  95. $this->points[] = $rad _in * cos (2*m_pi/5* ($i + 1)-Deg2rad ($ang _in)) + $this->centerdot[' x '];
  96. $this->points[] = $rad _in * sin (2*m_pi/5* ($i + 1)-Deg2rad ($ang _in)) + $this->centerdot[' y '];
  97. }
  98. Imagefilledpolygon ($this->img, $this->points, $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. External generation
  107. Public Function doimg () {
  108. $this->createimg ();
  109. $this->drawrim ();
  110. $this->drawinnercircle ();
  111. $this->drawstring ();
  112. $this->drawstart ();
  113. $this->output ();
  114. }
  115. }
Copy Code
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.