-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThumbnail.php
More file actions
294 lines (272 loc) · 9.88 KB
/
Thumbnail.php
File metadata and controls
294 lines (272 loc) · 9.88 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
class Thumbnail {
private $module;
private $moduleName;
private $uid;
private $configWidth;
private $configHeight;
private $originalWidth;
private $originalHeight;
private $cropWidth;
private $cropHeight;
private $cropX;
private $cropY;
private $filename;
private function GetProperty($property, $int = true) {
$ret = $this->$property;
if ($int !== false) $ret = (int)$ret;
return $ret;
}
private function GetSiteId() {
return NetDesign::GetInstance()->GetSiteId();
}
/**
* Constructor.
*
* @param NetDesign $module The module associated with the thumbnail.
* @param $uid The unique identifier for this thumbnail (and module).
* @param int $width The thumbnail width.
* @param int $height The thumbnail height.
*/
public function __construct(NetDesign $module, $uid, $width = 200, $height = 120) {
$this->module = $module;
$this->moduleName = get_class($module);
$this->uid = $uid;
$this->configWidth = (int)$width;
$this->configHeight = (int)$height;
// Load database entry
$te = ThumbnailEditor::GetInstance();
$db = $te->db;
$table = $te->GetTable();
$ret = $db->GetArray("SELECT * FROM `$table` WHERE `site_id` = ? AND `module` = ? AND `uid` = ? LIMIT 1", array(
$te->GetSiteId(), $this->GetModuleName(), $this->GetUid()
));
if (empty($ret)) return;
foreach($ret[0] as $prop => $value) {
$string = array('filename');
$int = array('originalWidth', 'originalHeight', 'cropWidth', 'cropHeight', 'cropX', 'cropY');
if (!in_array($prop, $string) && !in_array($prop, $int)) continue;
if (in_array($prop, $int)) $this->$prop = (int)$value;
else $this->$prop = $value;
}
}
/**
* Returns the associated module.
*
* @return NetDesign
*/
public function GetModule() {
return $this->GetProperty('module', false);
}
/**
* Returns the name of the associated module.
*
* @return string
*/
public function GetModuleName() {
return $this->GetProperty('moduleName', false);
}
/**
* Returns the (original) filename of the uploaded image.
*
* @return string
*/
public function GetFileName() {
return $this->GetProperty('filename', false);
}
/**
* Returns the unique identifier for this thumbnail.
*
* @return string
*/
public function GetUid() {
return $this->GetProperty('uid', false);
}
/**
* Returns the width of the resulting thumbnail.
*
* @return int
*/
public function GetConfigWidth() {
return $this->GetProperty('configWidth');
}
/**
* Returns the height of the resulting thumbnail.
*
* @return int
*/
public function GetConfigHeight() {
return $this->GetProperty('configHeight');
}
public function GetOriginalWidth() {
return $this->GetProperty('originalWidth');
}
/**
* Returns the height of the originally uploaded image.
*
* @return int
*/
public function GetOriginalHeight() {
return $this->GetProperty('originalHeight');
}
/**
* Returns the width to which the thumbnail should be resized before cropping.
*
* @return int
*/
public function GetCropWidth() {
return $this->GetProperty('cropWidth');
}
/**
* Returns the height to which the thumbnail should be resized before cropping.
*
* @return int
*/
public function GetCropHeight() {
return $this->GetProperty('cropHeight');
}
/**
* Returns the X-position at which to crop (after resizing).
*
* @return int
*/
public function GetCropX() {
return $this->GetProperty('cropX');
}
/**
* Returns the Y-position at which to crop (after resizing).
*
* @return int
*/
public function GetCropY() {
return $this->GetProperty('cropY');
}
/**
* Updates the thumbnail.
*
* @param int $width The width to which the image should be resized before cropping.
* @param int $height The height to which the thumbnail should be resized before cropping.
* @param int $x The X-position at which to crop after resizing.
* @param int $y The Y-position at which to crop after resizing.
*/
public function Crop($width, $height, $x, $y) {
$this->cropWidth = (int)$width;
$this->cropHeight = (int)$height;
$this->cropX = (int)$x;
$this->cropY = (int)$y;
// Save to the database
$te = ThumbnailEditor::GetInstance();
$db = $te->db;
$table = $te->GetTable();
$db->Execute("REPLACE INTO `$table` VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array(
$te->GetSiteId(), $this->GetModuleName(), $this->GetUid(), $this->GetConfigWidth(), $this->GetConfigHeight(),
$this->GetFileName(), $this->GetOriginalWidth(), $this->GetOriginalHeight(), $this->GetCropWidth(),
$this->GetCropHeight(), $this->GetCropX(), $this->GetCropY()
));
// Create the thumbnail
$img = $this->GetOriginalPath();
list($width, $height) = getimagesize($img);
$nwidth = $this->GetCropWidth();
$nheight = $this->GetCropHeight();
$im = imagecreatefromstring(file_get_contents($img));
$im2 = imagecreatetruecolor((int)$nwidth, (int)$nheight);
#printf("Resizing to %dx%d\n", $nwidth, $nheight);
imagecopyresampled($im2, $im, 0, 0, 0, 0, $nwidth, $nheight, $width, $height);
#printf("Cropping at %d,%d to a size of %dx%d\n", $this->GetCropX(), $this->GetCropY(), $this->GetConfigWidth(), $this->GetConfigHeight());
if (function_exists('imagecrop')) {
$im3 = imagecrop($im2, array('x' => $this->GetCropX(), 'y' => $this->GetCropY(), 'width' => $this->GetConfigWidth(), 'height' => $this->GetConfigHeight()));
} else {
$im3 = imagecreatetruecolor($this->GetConfigWidth(), $this->GetConfigHeight());
imagecopy($im3, $im2, 0, 0, $this->x, $this->y, $this->GetConfigWidth(), $this->GetConfigHeight());
}
$fn = $this->GetThumbnailPath();
@mkdir(dirname($fn), 0755, true);
@unlink($fn);
imagejpeg($im3, $fn);
}
/**
* Perform auto cropping, this will resize the image to the minimum needed dimensions, and then crop the center of the image.
*/
public function CropAuto() {
$width = $this->GetOriginalWidth();
$height = $this->GetOriginalHeight();
$ratio = max($this->GetConfigWidth() / $width, $this->GetConfigHeight() / $height);
$width = (int)($width * $ratio);
$height = (int)($height * $ratio);
$x = (int)(($width - $this->GetConfigWidth()) / 2);
$y = (int)(($height - $this->GetConfigHeight()) / 2);
$this->Crop($width, $height, $x, $y);
}
/**
* Returns the filesystem path to the originally uploaded image.
*
* @return string
*/
public function GetOriginalPath() {
return cms_join_path(ThumbnailEditor::GetInstance()->GetModuleUploadsPath(), $this->GetModuleName(), $this->GetUid(), 'original', $this->GetFileName());
}
/**
* Returns the filesystem path to the thumbnail image.
*
* @return string
*/
public function GetThumbnailPath() {
return cms_join_path(ThumbnailEditor::GetInstance()->GetModuleUploadsPath(), $this->GetModuleName(), $this->GetUid(), 'thumbnail', $this->GetFileName());
}
/**
* Returns the URL to the originally uploaded image.
*
* @return string
*/
public function GetOriginalUrl() {
if (!is_file($this->GetOriginalPath())) return '';
return cms_join_path(ThumbnailEditor::GetInstance()->GetModuleUploadsUrl(), $this->GetModuleName(), $this->GetUid(), 'original', $this->GetFileName()) . '?dt=' . (int)(microtime(true) * 1000);
}
/**
* Returns the URL to the thumbnail image.
*
* @return string
*/
public function GetThumbnailUrl() {
if (!is_file($this->GetThumbnailPath())) return '';
return cms_join_path(ThumbnailEditor::GetInstance()->GetModuleUploadsUrl(), $this->GetModuleName(), $this->GetUid(), 'thumbnail', $this->GetFileName()) . '?dt=' . (int)(microtime(true) * 1000);
}
/**
* Uploads a new image.
*
* @param string $source The filesystem path to the image.
* @param null|string $filename The destination filename. If omitted the filename of $source will be used.
*/
public function Upload($source, $filename = null) {
// Remove previous files
$or = $this->GetOriginalPath();
$th = $this->GetThumbnailPath();
if (is_file($or)) unlink($or);
if (is_file($th)) unlink($th);
// Upload new files
if (empty($filename)) $filename = basename($source);
$this->filename = $filename;
$path = $this->GetOriginalPath();
@mkdir(dirname($path), 0755, true);
copy($source, $path);
list($this->originalWidth, $this->originalHeight) = getimagesize($path);
// Create the default thumbnail
$this->CropAuto();
}
/**
* Returns TRUE if a thumbnail has been uploaded, FALSE otherwise.
*
* @return bool
*/
public function HasThumbnail() {
return is_file($this->GetThumbnailPath());
}
/**
* Short for GetThumbnailUrl().
*
* @return string
*/
public function url() {
return $this->GetThumbnailUrl();
}
}