首頁 >後端開發 >php教程 >實作中文圓形印章的PHP類

實作中文圓形印章的PHP類

WBOY
WBOY原創
2016-07-25 08:43:121128瀏覽
  1. /*
  2. * 中文圓形印章類
  3. * @author lkk/lianq.net
  4. * @create on 10:03 2012-5-29
  5. * @example:
  6. * $seal = new circleSeal('你我他坐站走東西南北中',75,6,24,0,0,16,40);
  7. * $seal->doImg();
  8. */
  9. class circleSeal {
  10. private $sealString; //印章字元
  11. private $strMaxLeng; //最大字元長度 private $sealRadius; //印章半徑
  12. private $rimWidth; //邊框厚度
  13. private $innerRadius; //內圓半徑
  14. private $startRadius; //五角星半徑
  15. private $startAngleglevate $startRadius ; //五角星傾斜角度
  16. private $backGround; //印章顏色
  17. private $centerDot; //圓心座標
  18. private $img; //圖形資源句柄
  19. private $font; //指定的字體
  20. private $fontSize; //指定字體大小
  21. private $width; //圖片寬度
  22. private $height; //圖片高度
  23. private $points; //五角星各點座標
  24. private $charRadius; //字串半徑
  25. private $charAngle; //字串傾斜角度
  26. private $spacing; //字元間隔角度
  27. //建構方法
  28. public function __construct($str ='', $rad = 75, $rmwidth = 6, $strad = 24, $stang = 0, $crang = 0, $fsize = 16, $inrad =0){
  29. $this->sealString = empty($str) ? '印章測試字串' : $str;
  30. $this->strMaxLeng = 12;
  31. $this->sealRadius = $rad;
  32. $this ->rimWidth = $rmwidth;
  33. $this->startRadius = $strad;
  34. $this->startAngle = $stang;
  35. $this->charAngle = $crang;
  36. $this-> centerDot = array('x'=>$rad, 'y'=>$rad);
  37. $this->font = dirname(__FILE__) .'/simkai.ttf';
  38. $this->fontSize = $fsize;
  39. $this->innerRadius = $inrad; //預設0,沒有
  40. $this->spacing = 1;
  41. }
  42. //建立圖片資源
  43. private function createImg(){
  44. $this->width = 2 * $this->sealRadius;
  45. $this->height = 2 * $this->sealRadius;
  46. $this->img = 。 0);
  47. }
  48. //畫印章邊框
  49. private function drawRim(){
  50. for($i=0;$irimWidth;$i ){
  51. imagearc($this->img,$this->centerDot['x'],$this->centerDot['y'],$this->width - $i,$this->height - $i ,0,360,$this->backGround);
  52. }
  53. }
  54. //畫內圓
  55. private function drawInnerCircle(){
  56. imagearc($this-this>img,$ this->centerDot['x'],$this->centerDot['y'],2*$this->innerRadius,2*$this->innerRadius,0,360,$this->backGround);
  57. }
  58. //畫字串
  59. private function drawString(){
  60. //編碼處理
  61. $charset = mb_detect_encoding($this->sealString);
  62. if($charset ! = 'UTF-8'){
  63. $this->sealString = mb_convert_encoding($this->sealString, 'UTF-8', 'GBK');
  64. }
  65. //相關計量
  66. $this->charRadius = $this->sealRadius - $this->rimWidth - $this->fontSize; //字串半徑
  67. $leng = mb_strlen($this->sealString,'utf8') ; //字串長度
  68. if($leng > $this->strMaxLeng) $leng = $this->strMaxLeng;
  69. $avgAngle = 360 / ($this->strMaxLeng); //平均字元傾斜度
  70. //拆分並寫入字串
  71. $words = array(); //字元陣列
  72. for($i=0;$i $words[] = mb_substr($this->sealString,$i,1,'utf8');
  73. $r = 630 $this->charAngle $avgAngle*($i - $leng/2) $this->spacing*($i-1); //座標角度
  74. $R = 720 - $this->charAngle $avgAngle*($leng-2*$i-1)/2 $this-> spacing*(1-$i); //字元角度
  75. $x = $this->centerDot['x'] $this->charRadius * cos(deg2rad($r)); //字元的x座標
  76. $y = $this->centerDot['y'] $this->charRadius * sin(deg2rad($r)); //字元的y座標
  77. imagettftext($this->img, $this ->fontSize, $R, $x, $y, $this->backGround, $this->font, $words[$i]);
  78. }
  79. }
  80. //繪圖方法
  81. private function drawStart(){
  82. $ang_out = 18 $this->startAngle;
  83. $ang_in = 56 $this->startAngle;
  84. $ rad_out = $this -> startRadius ;
  85. $rad_in = $rad_out * 0.382 ; this -> 點[ ] = $rad_out * cos ( 2 * M_PI / 5 * $i - deg2rad ( $ ang_out ) ) $this - > centerDot [ ' x ' ] ; rad_out * sin(2*M_PI/5*$i - deg2rad($ang_out)) $this->centerDot['y']; 點[] = $rad_in * cos(2*M_PI/5 *($i1) - deg2rad($ang_in)) $this->centerDot['x'];
  86. $this->points[] = $ rad_in * sin ( 2 * M_PI / 5 * ( $ i 1 ) - deg2rad ( $ ang_in ) ) $ this -> centerDot [ ' y ' ] ; ->points, 10, $this->backGround);
  87. }
  88. // key
  89. 私有 outPut (){
  90. header('Content-type:image/png');
  91. imagepng($this->img);
  92. imagedestroy($this->img); ();
  93. $ this->drawRim();
  94. $this->drawInnerCircle() ;
  95. $this->drawString(); ();
  96. $this->outPut();
  97. }
  98. }
  99. PHP
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn