




都是寫死的,代碼重復太多.沒有搜到橢圓的文字均勻分布的,數學也不好,有哪位大神能算一下.圓角框就是重疊套起來的,度娘沒找到php直接畫圓角框的,太麻煩了,不過還好做出來了,有更方便的求推薦.
function signtest(){
$name = "北京市阿斯達四大有限責任公司";
$path = $_SERVER["DOCUMENT_ROOT"] . '/data/sign/' . date("Y") . "/" . date("m") . "/". date("d") . "/";
mkdir($path,0755,true);
$filename = uniqid();
$picpath = $path . $filename;
Vendor('signcom');
$cc = new \circleSeal($name);
$sealname = '';
for($i=1;$i<4;$i++){
if($i==2) {
$sealname = '財務專用章';
}elseif($i==3){
$sealname = '合同專用章';
}
$cc->saveImg($picpath.".$i.jpg",$sealname);
}
$cc->saveEll($picpath.".4.jpg",'發票專用章');
$cc->saveSquare($picpath.".5.jpg",'王麗');
}
<?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 $numRadius; //數字半徑
private $charAngle; //字符串傾斜角度
private $spacing; //字符間隔角度
private $sealNum; //數字
private $sealName; //章名字
private $yheight; //字符的y坐標;
//構造方法
public function __construct($str ='',$num = '',$rad = 100, $rmwidth = 6, $strad = 28, $stang = 0, $crang = 0, $fsize = 16, $inrad =0){
$this->sealString = empty($str) ? '印章測試字符串' : $str;
$this->sealNum = empty($num) ? '010101010' : $num;
$this->strMaxLeng = 18;
$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__) .'/simhei.ttf';
$this->fontSize = $fsize;
$this->innerRadius = $inrad; //默認0,沒有
$this->spacing = 1;
$this->width = 2 * $this->sealRadius;
$this->height = 2 * $this->sealRadius;
$this->yheight = 150; //字符的y坐標
}
//創建圖片資源
private function createImg(){
$this->img = imagecreate($this->width, $this->height);
imagecolorresolvealpha($this->img,255,255,255,127);
$this->backGround = imagecolorallocate($this->img,255,0,0);
}
//畫印章邊框imagerectangle
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 drawSqu(){
for($i=0;$i<$this->rimWidth;$i++){
imagerectangle($this->img,0+$i,0+$i,120-$i,120-$i,$this->backGround);
}
}
//畫正方形的名字
private function drawName1(){
//編碼處理
$this->sealName .= '印';
$charset = mb_detect_encoding($this->sealName);
if($charset != 'UTF-8'){
$this->sealName = mb_convert_encoding($this->sealName, 'UTF-8', 'GBK');
}
//相關計量
$leng = mb_strlen($this->sealName,'utf8'); //字符串長度
$nums = array();
//拆分並寫入字符串
for($i=0;$i<$leng;$i++){
$nums[] = mb_substr($this->sealName,$i,1,'utf8');
}
imagettftext($this->img,35, 0, 60,50, $this->backGround, $this->font, $nums[0]);
imagettftext($this->img,35, 0, 60,105, $this->backGround, $this->font, $nums[1]);
if($leng==3){
imagettftext($this->img,35, 0, 10,77.5, $this->backGround, $this->font, $nums[2]);
}else{
imagettftext($this->img,35, 0, 10,50, $this->backGround, $this->font, $nums[2]);
imagettftext($this->img,35, 0, 10,105, $this->backGround, $this->font, $nums[3]);
}
}
//畫內圓
private function drawInnerCircle(){
imagearc($this->img,$this->centerDot['x'],$this->centerDot['y'],2*$this->innerRadius,2*$this->innerRadius,0,360,$this->backGround);
}
//畫下面的數字
private function drawNum(){
//編碼處理
$charset = mb_detect_encoding($this->sealNum);
if($charset != 'UTF-8'){
$this->sealNum = mb_convert_encoding($this->sealNum, 'UTF-8', 'GBK');
}
//相關計量
$this->numRadius = $this->sealRadius - $this->rimWidth - 5; //數字半徑
$leng = mb_strlen($this->sealNum,'utf8'); //字符串長度
// if($leng > $this->strMaxLeng) $leng = $this->strMaxLeng;
$avgAngle = 80 / 9; //平均字符傾斜度
//拆分並寫入字符串
$nums = array(); //字符數組
for($i=0;$i<$leng;$i++){
$nums[] = mb_substr($this->sealNum,$i,1,'utf8');
$r =95 + $this->charAngle + $avgAngle*($i - $leng/2); //坐標角度
$R = 720 - $this->charAngle + $avgAngle*($leng-2*$i-1)/2 ; //字符角度
$x = $this->centerDot['x'] + $this->numRadius * cos(deg2rad($r)); //字符的x坐標
$y = $this->centerDot['y'] + $this->numRadius * sin(deg2rad($r)); //字符的y坐標
imagettftext($this->img,10, $R, $x, $y, $this->backGround, $this->font, $nums[$i]);
}
}
//畫中間章的名字
private function drawName(){
//編碼處理
$charset = mb_detect_encoding($this->sealName);
if($charset != 'UTF-8'){
$this->sealName = mb_convert_encoding($this->sealName, 'UTF-8', 'GBK');
}
//相關計量
$leng = mb_strlen($this->sealName,'utf8'); //字符串長度
//拆分並寫入字符串
$nums = array(); //字符數組
for($i=0;$i<$leng;$i++){
$nums[] = mb_substr($this->sealName,$i,1,'utf8');
// $r =99 + $this->charAngle + $avgAngle*($i - $leng/2); //坐標角度
// $R = 720 - $this->charAngle + $avgAngle*($leng-2*$i-1)/2 ; //字符角度
$x = 44+$i*22; //字符的x坐標
$y = $this->yheight; //字符的y坐標
imagettftext($this->img,18, 0, $x, $y, $this->backGround, $this->font, $nums[$i]);
}
}
//畫字符串
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-5; //字符串半徑
$leng = mb_strlen($this->sealString,'utf8'); //字符串長度
if($leng > $this->strMaxLeng) $leng = $this->strMaxLeng;
$avgAngle = 270 / ($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); //坐標角度
$R = 720 - $this->charAngle + $avgAngle*($leng-2*$i-1)/2 ; //字符角度
$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 drawString1(){
//編碼處理
$charset = mb_detect_encoding($this->sealString);
if($charset != 'UTF-8'){
$this->sealString = mb_convert_encoding($this->sealString, 'UTF-8', 'GBK');
}
//相關計量
$charRadiusy = $this->centerDot['y'] - $this->rimWidth - $this->fontSize-2; //字符串y
$charRadiusx = $this->centerDot['x'] - $this->rimWidth - $this->fontSize-2; //字符串x
$leng = mb_strlen($this->sealString,'utf8'); //字符串長度
if($leng > $this->strMaxLeng) $leng = $this->strMaxLeng;
$avgAngle = 270 / $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); //坐標角度
$R = 720- $this->charAngle + $avgAngle*($leng-2*$i-1)/2; //字符角度
$x = $this->centerDot['x'] + $charRadiusx * cos(deg2rad($r))+2; //字符的x坐標
$y = $this->centerDot['y'] + $charRadiusy * sin(deg2rad($r)); //字符的y坐標
imagettftext($this->img, $this->fontSize, $R, $x, $y, $this->backGround, $this->font,$words[$i]);
}
}
//畫下面的數字
private function drawNum1(){
//編碼處理
$charset = mb_detect_encoding($this->sealNum);
if($charset != 'UTF-8'){
$this->sealNum = mb_convert_encoding($this->sealNum, 'UTF-8', 'GBK');
}
//相關計量
$charRadiusy = $this->centerDot['y'] - $this->rimWidth - $this->fontSize+15; //字符串y
$charRadiusx = $this->centerDot['x'] - $this->rimWidth - $this->fontSize-2; //字符串x
$leng = mb_strlen($this->sealNum,'utf8'); //字符串長度
// if($leng > $this->strMaxLeng) $leng = $this->strMaxLeng;
$avgAngle = 80 / 9; //平均字符傾斜度
//拆分並寫入字符串
$nums = array(); //字符數組
for($i=0;$i<$leng;$i++){
$nums[] = mb_substr($this->sealNum,$i,1,'utf8');
$r =97 + $this->charAngle + $avgAngle*($i - $leng/2); //坐標角度
$R = 720 - $this->charAngle + $avgAngle*($leng-2*$i-1)/2 ; //字符角度
$x = $this->centerDot['x'] + $charRadiusx * cos(deg2rad($r)); //字符的x坐標
$y = $this->centerDot['y'] + $charRadiusy * sin(deg2rad($r)); //字符的y坐標
imagettftext($this->img,10, $R, $x, $y, $this->backGround, $this->font, $nums[$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 saveImg($path,$name=''){
$this->sealName = $name;
$this->createImg();
$this->drawRim();
$this->drawInnerCircle();
$this->drawName();
$this->drawNum();
$this->drawString();
$this->drawStart();
imagepng($this->img,$path);
imagedestroy($this->img);
}
//生成橢圓
public function saveEll($path,$name=''){
$this->sealName = $name;
$this->width = 200;
$this->height = 147;
$this->yheight = 110;
$this->startRadius = 20;
$this->fontSize = 14;
$this->sealRadius = 75;
$this->points = array();
$this->centerDot = array('x'=>$this->width/2, 'y'=>$this->height/2);
$this->createImg();
$this->drawRim();
$this->drawInnerCircle();
$this->drawName();
$this->drawNum1();
$this->drawString1();
$this->drawStart();
imagepng($this->img,$path);
imagedestroy($this->img);
// $this->outPut();
}
//生成圓角正方形
public function saveSquare($path,$name=''){
$this->sealName = $name;
// $this->width = 120;
// $this->height = 120;
// $this->createImg();
// $this->drawSqu();
// $this->drawName1();
// $this->outPut();
$this->width = 120;
$this->height = 120;
$this->img = imagecreatetruecolor($this->width, $this->width); // 創建一個正方形的圖像
$bgcolor = imagecolorallocate($this->img, 255, 0, 0); // 圖像的背景
imagefill($this->img, 0, 0, $bgcolor);
// 圓角處理
$radius = 5;
// lt(左上角)
$lt_corner = $this->get_lt_rounder_corner($radius);
imagecopymerge($this->img, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
// lb(左下角)
$lb_corner = imagerotate($lt_corner, 90, 0);
imagecopymerge($this->img, $lb_corner, 0, $this->height - $radius, 0, 0, $radius, $radius, 100);
// rb(右上角)
$rb_corner = imagerotate($lt_corner, 180, 0);
imagecopymerge($this->img, $rb_corner, $this->width - $radius, $this->height - $radius, 0, 0, $radius, $radius, 100);
// rt(右下角)
$rt_corner = imagerotate($lt_corner, 270, 0);
imagecopymerge($this->img, $rt_corner, $this->width - $radius, 0, 0, 0, $radius, $radius, 100);
$img1 = imagecreatetruecolor(110, 110); // 創建一個正方形的圖像
$bgcolor = imagecolorallocate($img1, 255, 255, 255); // 圖像的背景
imagefill($img1, 0, 0, $bgcolor);
// 圓角處理
$radius = 5;
// lt(左上角)
$lt_corner = $this->get_lt_rounder_corner($radius,255,1,0);
imagecopymerge($img1, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
// lb(左下角)
$lb_corner = imagerotate($lt_corner, 90, 0);
imagecopymerge($img1, $lb_corner, 0, 105, 0, 0, $radius, $radius, 100);
// rb(右上角)
$rb_corner = imagerotate($lt_corner, 180, 0);
imagecopymerge($img1, $rb_corner, 105, 105, 0, 0, $radius, $radius, 100);
// rt(右下角)
$rt_corner = imagerotate($lt_corner, 270, 0);
imagecopymerge($img1, $rt_corner,105, 0, 0, 0, $radius, $radius, 100);
imagecopymerge($this->img, $img1, 5, 5, 0, 0, 110, 110, 100);
$this->backGround = imagecolorallocate($this->img,255,0,0);
$this->drawName1();
imagepng($this->img,$path);
imagedestroy($this->img);
}
function get_lt_rounder_corner($radius,$r=255,$g=255,$b=255) {
$img = imagecreatetruecolor($radius, $radius); // 創建一個正方形的圖像
$bgcolor = imagecolorallocate($img, $r, $g, $b); // 圖像的背景
$fgcolor = imagecolorallocate($img, 255,0,0);
imagefill($img, 0, 0, $bgcolor);
// $radius,$radius:以圖像的右下角開始畫弧
// $radius*2, $radius*2:已寬度、高度畫弧
// 180, 270:指定了角度的起始和結束點
// fgcolor:指定顏色
imagefilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);
// 將弧角圖片的顏色設置為透明
imagecolortransparent($img, $fgcolor);
// 變換角度
// $img = imagerotate($img, 90, 0);
// $img = imagerotate($img, 180, 0);
// $img = imagerotate($img, 270, 0);
// header('Content-Type: image/png');
// imagepng($img);
return $img;
}
//對外生成
public function doImg($name=''){
$this->sealName = $name;
$this->createImg();
$this->drawRim();
$this->drawInnerCircle();
$this->drawName();
$this->drawNum();
$this->drawString();
$this->drawStart();
$this->outPut();
}
}
