欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP实现中文圆形印章特效

程序员文章站 2022-06-26 11:48:08
方法一:

方法一:

<?php
/*
 * 中文圆形印章类
 * @author lkk/lianq.net
 * @create on 10:03 2012-5-29
 * @example:
 * $seal = new circleseal('你我他坐站走东西南北中',75,6,24,0,0,16,40);
 * $seal->doimg();
 */
 
class circleseal {
  private $sealstring;  //印章字符
  private $strmaxleng;  //最大字符长度
  private $sealradius;  //印章半径
  private $rimwidth;   //边框厚度
  private $innerradius;  //内圆半径
  private $startradius;  //五角星半径
  private $startangle;  //五角星倾斜角度
  private $background;  //印章颜色
  private $centerdot;   //圆心坐标
  private $img;      //图形资源句柄
  private $font;     //指定的字体
  private $fontsize;   //指定字体大小
  private $width;     //图片宽度
  private $height;    //图片高度
  private $points;    //五角星各点坐标
  private $charradius;  //字符串半径
  private $charangle;   //字符串倾斜角度
  private $spacing;    //字符间隔角度
 
  //构造方法
  public function __construct($str ='', $rad = 75, $rmwidth = 6, $strad = 24, $stang = 0, $crang = 0, $fsize = 16, $inrad =0){
    $this->sealstring  = empty($str) ? '印章测试字符串' : $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;  //默认0,没有
    $this->spacing   = 1;
  }
 
  //创建图片资源
  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);
  }
 
  //画印章边框
  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);
    }
  }
 
  //画内圆
  private function drawinnercircle(){
    imagearc($this->img,$this->centerdot['x'],$this->centerdot['y'],2*$this->innerradius,2*$this->innerradius,0,360,$this->background);
  }
 
  //画字符串
  private function drawstring(){
    //编码处理
    $charset = mb_detect_encoding($this->sealstring);
    if($charset != 'utf-8'){
      $this->sealstring = mb_convert_encoding($this->sealstring, 'utf-8', 'gbk');
    }
 
    //相关计量
    $this->charradius = $this->sealradius - $this->rimwidth - $this->fontsize; //字符串半径
    $leng  = mb_strlen($this->sealstring,'utf8'); //字符串长度
    if($leng > $this->strmaxleng) $leng = $this->strmaxleng;
    $avgangle  = 360 / ($this->strmaxleng);  //平均字符倾斜度
 
    //拆分并写入字符串
    $words = 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);   //坐标角度
      $r = 720 - $this->charangle + $avgangle*($leng-2*$i-1)/2 + $this->spacing*(1-$i);  //字符角度
      $x = $this->centerdot['x'] + $this->charradius * cos(deg2rad($r)); //字符的x坐标
      $y = $this->centerdot['y'] + $this->charradius * sin(deg2rad($r)); //字符的y坐标
      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();
  }
}

方法二:

<?php
@$hos=iconv("gbk", "utf-8", $_get["hos"]);
if(!isset($hos))
exit;

$im=imagecreate(150,150);
$gray=imagecolorresolvealpha($im,200,200,200,127);
$red=imagecolorallocate($im,230,150,150);

for($i=0;$i<6;$i++)
imagearc($im,75,75,148-$i,148-$i,0,360,$red);

$stock='c:\windows\fonts\simkai.ttf';
$point="★";
$size=30;
imagettftext($im,$size,0,72-$size/2,72+$size/2,$red,$stock,$point);

$a=75;$b=-75;//中心点坐标
$r=65;$m=40;//半径,角度
$size=16;//字体大小
$r=$r-$size;

$word=array();
$max=18;
$count=mb_strlen($hos,'utf8');
if($count>$max)$count=$max;
if($count>12)
$m=floor(360/$count);
else if($count>5)
$m-=$count;

for($i=0;$i<$count;$i++)
$word[]=mb_substr($hos,$i,1,'utf8');

$j=floor($count/2);
if($j!=$count/2)
{
 for($i=$j;$i>=0;$i--)
 {
 $arc=$m*($j-$i)+$size/2;
 $x=round($r*cos((90+$arc)*m_pi/180))+$a;
 $y=-1*(round($r*sin((90+$arc)*m_pi/180))+$b);
 if($arc<10)$arc=0; 
 imagettftext($im,$size,$arc,$x,$y,$red,$stock,$word[$i]);
 $arc=$m*($j-$i)-$size/2;
 $x=round($r*cos((90-$arc)*m_pi/180))+$a;
 $y=-1*(round($r*sin((90-$arc)*m_pi/180))+$b);
 if($arc<10)$arc=0; 
 imagettftext($im,$size,-$arc,$x,$y,$red,$stock,$word[$j+$j-$i]);
 }
}
else
{
 $j=$j-1;
 for($i=$j;$i>=0;$i--)
 {
 $arc=$m/2+$m*($j-$i)+$size/2;
 $x=round($r*cos((90+$arc)*m_pi/180))+$a;
 $y=-1*(round($r*sin((90+$arc)*m_pi/180))+$b);
 imagettftext($im,$size,$arc,$x,$y,$red,$stock,$word[$i]); 
 $arc=$m/2+$m*($j-$i)-$size/2;
 $x=round($r*cos((90-$arc)*m_pi/180))+$a;
 $y=-1*(round($r*sin((90-$arc)*m_pi/180))+$b);
 imagettftext($im,$size,-$arc,$x,$y,$red,$stock,$word[$j+$j+1-$i]);
 }
}

header('content-type:image/png');
imagepng($im);
?>

以上所述就是本文的全部内容了,希望大家能够喜欢