- /*
- * Chinese round seal category
- * @author lkk/lianq.net
- * @create on 10:03 2012-5-29
- * @example:
- * $seal = new circleSeal('You and I sit and walk east, west, north, south, center',75,6,24,0,0,16,40);
- * $seal->doImg();
- */
-
- class circleSeal {
- private $sealString; //Seal characters
- private $strMaxLeng; //Maximum character length
- private $sealRadius; //Seal radius
- private $rimWidth; //Border thickness
- private $innerRadius; //Inner circle radius
- private $startRadius ; //Pentagram radius
- private $startAngle; //Pentagram tilt angle
- private $backGround; //Seal color
- private $centerDot; //Circle center coordinates
- private $img; //Graphic resource handle
- private $font; //Specified font
- private $fontSize; //Specified font size
- private $width; //Picture width
- private $height; //Picture height
- private $points; //Coordinates of each point of the five-pointed star
- private $charRadius; //String radius
- private $charAngle; //String tilt angle
- private $spacing; //Character spacing angle
-
- //Construction method
- public function __construct($str ='', $rad = 75, $rmwidth = 6, $strad = 24, $stang = 0, $crang = 0, $fsize = 16, $inrad =0){
- $this->sealString = empty($str) ? 'Seal test string' : $str;
- $this->strMaxLeng = 12;
- $this->sealRadius = $rad;
- $this->rimWidth = $rmwidth;
- $this->startRadius = $strad;
- $this- >startAngle = $stang;
- $this->charAngle = $crang;
- $this->centerDot = array('x'=>$rad, 'y'=>$rad);
- $this ->font = dirname(__FILE__) .'/simkai.ttf';
- $this->fontSize = $fsize;
- $this->innerRadius = $inrad; //Default 0, no
- $this-> ;spacing = 1;
- }
-
- //Create image resources
- private function createImg(){
- $this->width = 2 * $this->sealRadius;
- $this->height = 2 * $this ->sealRadius;
- $this->img = imagecreate($this->width, $this->height);
- imagecolorresolvealpha($this->img,255,255,255,127);
- $this->backGround = imagecolorallocate($this->img,255,0,0);
- }
-
- //Draw the seal border
- private function drawRim(){
- for($i=0;$i<$this->rimWidth ;$i++){
- imagearc($this->img,$this->centerDot['x'],$this->centerDot['y'],$this->width - $i,$ this->height - $i,0,360,$this->backGround);
- }
- }
-
- //Draw an inner circle
- private function drawInnerCircle(){
- imagearc($this->img,$this- >centerDot['x'],$this->centerDot['y'],2*$this->innerRadius,2*$this->innerRadius,0,360,$this->backGround);
- }
-
- //Draw a string
- private function drawString(){
- //Encoding processing
- $charset = mb_detect_encoding($this->sealString);
- if($charset != 'UTF-8'){
- $ this->sealString = mb_convert_encoding($this->sealString, 'UTF-8', 'GBK');
- }
-
- //Related measurement
- $this->charRadius = $this->sealRadius - $ this->rimWidth - $this->fontSize; //String radius
- $leng = mb_strlen($this->sealString,'utf8'); //String length
- if($leng > $this ->strMaxLeng) $leng = $this->strMaxLeng;
- $avgAngle = 360 / ($this->strMaxLeng); //Average character tilt
-
- //Split and write string
- $words = array(); //Character array
- for($i=0;$i<$leng;$i++){
- $words[] = mb_substr($this->sealString,$i,1,'utf8' );
- $r = 630 + $this->charAngle + $avgAngle*($i - $leng/2) + $this->spacing*($i-1); //Coordinate angle
- $R = 720 - $this->charAngle + $avgAngle*($leng-2*$i-1)/2 + $this->spacing*(1-$i); //Character angle
- $x = $this ->centerDot['x'] + $this->charRadius * cos(deg2rad($r)); //The x coordinate of the character
- $y = $this->centerDot['y'] + $this ->charRadius * sin(deg2rad($r)); //The y coordinate of the character
- imagettftext($this->img, $this->fontSize, $R, $x, $y, $this-> ;backGround, $this->font, $words[$i]);
- }
- }
-
- //画五角星
- private function drawStart(){
- $ang_out = 18 + $this->startAngle;
- $ang_in = 56 + $this->startAngle;
- $rad_out = $this->startRadius;
- $rad_in = $rad_out * 0.382;
- for($i=0;$i<5;$i++){
- //五个顶点坐标
- $this->points[] = $rad_out * cos(2*M_PI/5*$i - deg2rad($ang_out)) + $this->centerDot['x'];
- $this->points[] = $rad_out * sin(2*M_PI/5*$i - deg2rad($ang_out)) + $this->centerDot['y'];
-
- //内凹的点坐标
- $this->points[] = $rad_in * cos(2*M_PI/5*($i+1) - deg2rad($ang_in)) + $this->centerDot['x'];
- $this->points[] = $rad_in * sin(2*M_PI/5*($i+1) - deg2rad($ang_in)) + $this->centerDot['y'];
- }
- imagefilledpolygon($this->img, $this->points, 10, $this->backGround);
- }
-
- //输出
- private function outPut(){
- header('Content-type:image/png');
- imagepng($this->img);
- imagedestroy($this->img);
- }
-
- //对外生成
- public function doImg(){
- $this->createImg();
- $this->drawRim();
- $this->drawInnerCircle();
- $this->drawString();
- $this->drawStart();
- $this->outPut();
- }
- }
-
复制代码
|