RoundCorner.class.php
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
class Util_Image_RoundCorner
{
private $im;
private $imageInfo;
private $radius;
private $rColor;
private $bColor;
private $gColor;
private $imageTypes = array(
1 => 'GIF',2 => 'JPG',3 => 'PNG',4 => 'SWF',
5 => 'PSD',6 => 'BMP',7 => 'TIFF(intel byte order)',
8 => 'TIFF(motorola byte order)',9 => 'JPC',10 => 'JP2',
11 => 'JPX',12 => 'JB2',13 => 'SWC',14 => 'IFF',15 => 'WBMP',
16 => 'XBM'
);
/**
* 初始化
*
* @param sring $image
* @param int $radius
* @param int $rColor
* @param int $bColor
* @param int $gColor
*
*/
public function __construct($image, $radius, $rColor=255, $bColor=0, $gColor=0)
{
if(file_exists($image))
{
$imageInfo = getimagesize($image);
$this->imageInfo = array
(
'file' => str_replace('\\', '/', realpath($image)),
'width' => $imageInfo[0],
'height' => $imageInfo[1],
'type' => $this->imageTypes[$imageInfo[2]],
'mime' => $imageInfo['mime']
);
//获取原图
$this->im = $this->getLoadImage($image, $this->imageInfo['type']);
$this->radius = $radius;
$this->rColor = $rColor;
$this->bColor = $bColor;
$this->gColor = $bColor;
}
else
{
exit();
}
}
/**
* 加载图片
*
* @param string $image
* @param string $type
* @return gd
*/
private function getLoadImage($image, $type)
{
if (empty($type))
{
return false;
}
switch(strtolower($type))
{
case 'jpg':
$img = @imagecreatefromjpeg($image);
break;
case 'gif':
$img = @imagecreatefromgif($image);
break;
case 'png':
$img = @imagecreatefrompng($image);
break;
default:
return false;
}
return $img;
}
/**
* 获取圆角图片
*/
private function getCornerImage()
{
//画布
$cornerImage = imagecreatetruecolor($this->radius, $this->radius);
//背景
$bgColor = imagecolorallocate($cornerImage, $this->rColor, $this->gColor, $this->bColor);
//前景
$fgColor = imagecolorallocate($cornerImage, 0, 0, 0);
imagefill($cornerImage, 0,0 ,$bgColor);
//四分之一圆
imagefilledarc($cornerImage, $this->radius, $this->radius, $this->radius*2, $this->radius*2, 180, 270, $fgColor, IMG_ARC_PIE);
imagecolortransparent($cornerImage, $fgColor);
return $cornerImage;
}
/**
* 圆角
*/
public function rounded()
{
$cornerImage = $this->getCornerImage();
$rounderImage = imagecreatetruecolor($this->imageInfo['width'], $this->imageInfo['height']);
$fgColor = imagecolorallocate($rounderImage, $this->rColor, $this->gColor, $this->bColor);
imagefill($rounderImage, 0, 0, $fgColor);
imagecopymerge($rounderImage, $this->im, 0, 0,0,0,$this->imageInfo['width'], $this->imageInfo['height'], 100 );
imagecopymerge($rounderImage, $cornerImage,0,0,0,0, $this->radius,$this->radius,100);
//左底
$lbRounderImage = imagerotate($cornerImage, 90, $fgColor);
imagecopymerge($rounderImage, $lbRounderImage, 0, $this->imageInfo['height']-$this->radius, 0, 0,$this->radius,$this->radius, 100);
//右底
$rbCornerImage = imagerotate($cornerImage, 180, $fgColor);
imagecopymerge($rounderImage, $rbCornerImage, $this->imageInfo['width']-$this->radius, $this->imageInfo['height']-$this->radius, 0, 0, $this->radius, $this->radius, 100);
//左上
$ltCornerImage = imagerotate($cornerImage, 270, $fgColor);
imagecopymerge($rounderImage, $ltCornerImage, $this->imageInfo['width']-$this->radius, 0, 0, 0, $this->radius, $this->radius,100);
imagecolortransparent($rounderImage, $fgColor);
//新图片
$ext = substr($this->imageInfo['file'], strrpos($this->imageInfo['file'], '.'));
$rounderName = str_replace($ext, '', $this->imageInfo['file']).'_round.png';
imagepng($rounderImage, $rounderName);
}
}