<?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 $backGround; //印章颜色
private $centerDot; //圆心坐标
private $img; //图形资源句柄
private $font; //指定的字体
private $fontSize; //指定字体大小
private $width; //图片宽度
private $height; //图片高度
private $charRadius; //字符串半径
private $charAngle; //字符串倾斜角度
private $spacing; //字符间隔角度
//构造方法
public function __construct($str = '', $rad = 97.5, $crang = 0, $fsize = 10) {
$this->sealString = empty($str) ? '印章测试字符串' : $str;
$this->strMaxLeng = 12;
$this->sealRadius = $rad;
$this->charAngle = $crang;
$this->centerDot = array('x' => $rad, 'y' => $rad);
$this->font = ROOT_PATH . 'public' . DS . 'File_' . DS . 'StorNameCircle' . DS . 'wryh.ttf';
$this->fontSize = $fsize;
$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, 255, 255);
}
//画字符串
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->fontSize - 15; //字符串半径
$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 outPut() {
header('Content-type:image/png');
$uid = uniqid();
imagepng($this->img, ROOT_PATH . 'public' . DS . 'File_' . DS . 'StorNameCircle' . DS . $uid . '.png');
$image = imagecreatefromstring(file_get_contents(ROOT_PATH . 'public' . DS . 'File_' . DS . 'StorNameCircle' . DS . $uid . '.png'));
$desurl = ROOT_PATH . 'public' . DS . 'File_' . DS . 'StorNameCircle' . DS . 'red_w.png'; //覆盖图路径
$a = imagecreatefromstring(file_get_contents($desurl));
imagecopy($a, $image, 0, 0, 0, 0, 195, 195);
imagepng($a, ROOT_PATH . 'public' . DS . 'File_' . DS . 'StorNameCircle' . DS . 'prepare.png');
$url = ROOT_PATH . 'public' . DS . 'File_' . DS . 'StorNameCircle' . DS . 'prepare.png';
imagedestroy($image);
imagedestroy($a);
imagedestroy($this->img);
return $this->circle($url, ROOT_PATH . 'public' . DS . 'File_' . DS . 'StorNameCircle' . DS);
}
public function circle($url, $path = './') {
$w = 195;
$h = 195; // original size
$original_path = $url;
// dump($path);
$dest_path = $path . uniqid() . '.png';
// dump($dest_path);
$src = imagecreatefromstring(file_get_contents($original_path));
$newpic = imagecreatetruecolor($w, $h);
imagealphablending($newpic, false);
$transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);
$r = $w / 2;
for ($x = 0; $x < $w; $x++)
for ($y = 0; $y < $h; $y++) {
$c = imagecolorat($src, $x, $y);
$_x = $x - $w / 2;
$_y = $y - $h / 2;
if ((($_x * $_x) + ($_y * $_y)) < ($r * $r)) {
imagesetpixel($newpic, $x, $y, $c);
} else {
imagesetpixel($newpic, $x, $y, $transparent);
}
}
imagesavealpha($newpic, true);
imagepng($newpic, $dest_path);
imagedestroy($newpic);
imagedestroy($src);
unlink($url);
return $dest_path;
}
//对外生成
public function doImg() {
$this->createImg();
$this->drawString();
$url = $this->outPut();
return $url;
// $this->
}
}
QQ:961052877