/
ImageGD.class.php
204 lines (168 loc) · 5.22 KB
/
ImageGD.class.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
class ImageGD
{
private $_src;
private $_type;
private $_columns;
private $_rows;
private $_valid;
public function ImageGD ($filename)
{
$info = getimagesize($filename);
if ($info == FALSE)
{
$this->_valid == FALSE;
return $this;
}
$this->_type = $info[2];
switch ($this->_type)
{
case IMAGETYPE_GIF:
$this->_src = imagecreatefromgif($filename);
break;
case IMAGETYPE_JPEG:
$this->_src = imagecreatefromjpeg($filename);
break;
case IMAGETYPE_PNG:
$this->_src = imagecreatefrompng($filename);
break;
default:
exit();
}
$this->_valid = TRUE;
$this->_columns = $info[0];
$this->_rows = $info[1];
}
public function valid ()
{
return $this->_valid;
}
public function writeImage ($filename)
{
if ($filename == NULL)
return;
switch ($this->_type)
{
case IMAGETYPE_GIF:
imagegif($this->_src, $filename);
break;
case IMAGETYPE_JPEG:
imagejpeg($this->_src, $filename);
break;
case IMAGETYPE_PNG:
imagepng($this->_src, $filename);
break;
default:
return;
}
}
public function cropImage ($columns, $rows)
{
$aspect = $columns/$rows;
list($columns_old, $rows_old) = $this->getCroppedAspectSize($this->_columns, $this->_rows, $aspect);
# Center the crop
$columns_center = round($this->_columns/2);
$rows_center = round($this->_rows/2);
$columns_old_origin = $columns_center - round($columns_old/2);
$rows_old_origin = $rows_center - round($rows_old/2);
$this->resizeImage($columns_old, $rows_old, $columns, $rows, $columns_old_origin, $rows_old_origin);
}
public function scaleImage ($columns, $rows)
{
if ($rows == 0)
$rows = $this->getScaledRows($columns);
if ($columns == 0)
$columns = $this->getScaledColumns($rows);
$this->resizeImage($this->_columns, $this->_rows, $columns, $rows);
}
public function __toString()
{
ob_start();
switch ($this->_type)
{
case IMAGETYPE_GIF:
imagegif($this->_src);
break;
case IMAGETYPE_JPEG:
imagejpeg($this->_src);
break;
case IMAGETYPE_PNG:
imagepng($this->_src);
break;
default:
echo '';
}
$imageData = ob_get_contents();
ob_end_clean();
return $imageData;
}
public function thumbnailImage ($columns, $rows)
{
$this->scaleImage($columns, $rows);
}
public function cropThumbnailImage ($columns, $rows)
{
$this->cropImage($columns, $rows);
}
private function resizeImage ($columns_old, $rows_old, $columns_new, $rows_new, $columns_old_origin = 0, $rows_old_origin = 0)
{
$newSrc = $this->getResizedSrc($columns_new, $rows_new);
imagecopyresampled ($newSrc, $this->_src, 0, 0, $columns_old_origin, $rows_old_origin, $columns_new, $rows_new, $columns_old, $rows_old);
# Set the new properties
$this->_src = $newSrc;
$this->_columns = $columns;
$this->_rows = $rows;
}
private function getResizedSrc ($columns, $rows)
{
$resizedSrc = imagecreatetruecolor( $columns, $rows );
if ($this->_type == IMAGETYPE_GIF
|| $this->_type == IMAGETYPE_PNG)
{
$transparentIndex = imagecolortransparent($this->_src);
// If we have a specific transparent color
if ($transparentIndex >= 0)
{
// Get the original image's transparent color's RGB values
$transparentColor = imagecolorsforindex($this->_src, $transparentIndex);
// Allocate the same color in the new image resource
$transparentIndex = imagecolorallocate($resizedSrc, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($resizedSrc, 0, 0, $transparentIndex);
// Set the background color for new image to transparent
imagecolortransparent($resizedSrc, $transparentIndex);
}
elseif ($imageType == IMAGETYPE_PNG)
{
imagealphablending($resizedSrc, false);
$color = imagecolorallocatealpha($resizedSrc, 0, 0, 0, 127);
imagefill($resizedSrc, 0, 0, $color);
imagesavealpha($resizedSrc, true);
}
}
return $resizedSrc;
}
private function getCroppedAspectSize ($columns, $rows, $aspect)
{
if ($columns < $rows*$aspect)
$rows = round($columns/$aspect);
else
$columns = round($rows*$aspect);
return array($columns, $rows);
}
private function getScaledColumns ($rows)
{
$columns = ($this->_columns * $rows) / $this->_rows;
if ($columns == 0)
$columns = $this->_columns;
return $columns;
}
private function getScaledRows ($columns)
{
$rows = ($this->_rows * $columns) / $this->_columns;
if ($rows == 0)
$rows == $this->_rows;
return $rows;
}
}
?>