-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathImageEdit.php
More file actions
350 lines (315 loc) · 8.67 KB
/
ImageEdit.php
File metadata and controls
350 lines (315 loc) · 8.67 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* ImageEdit
*
* Image manipulation using input from js editor.
*
* @author akirk
* @copyright Copyright (c) 2013
* @version 0.1
* @access public
*/
class ImageEdit {
public $cache_dir;
public $cache_id;
public $cache_name;
public $current_img;
public $errors;
public $ext;
public $fieldname;
public $has_image;
public $lib_uri;
public $max_size;
public $max_version;
public $saved;
public $savepath;
public $savename;
public $types;
public $version;
public function __construct(array $config = array())
{
$this->cache_dir = 'cache';
$this->errors = array();
$this->fieldname = 'image';
$this->max_size = 200 * 1024;
$this->max_version = 1;
$this->types = array(
'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg'
);
$this->saved = false;
$this->savepath = false;
$this->savename = false;
$this->version = 1;
$lib_dir = explode('/', $_SERVER['PHP_SELF']);
array_pop($lib_dir);
$this->lib_uri = implode('/', $lib_dir) . '/';
foreach ($config as $key=>$val) {
if (property_exists($this, $key)) {
$this->$key = $val;
}
}
// Check for writable cache dir:
if (!is_writable(($this->cache_dir))) {
trigger_error('Cache dir ' . $this->cache_dir . ' is not writable', E_USER_ERROR);
}
}
protected function buildCacheName($version)
{
return 'cache/' . $this->cache_id . '_' . $version . '.' . $this->ext;
}
protected function buildImagePath($version)
{
return $this->lib_uri . $this->buildCacheName($version);
}
/*protected function clearCache()
{
foreach (scandir('cache') as $file) {
echo "<pre>\n";var_dump($file);echo "</pre>\n";
if (strpos($file, $this->cache_id) !== false) {
unlink('cache' . DIRECTORY_SEPARATOR . $file);
}
}
}*/
protected function hasImage()
{
$this->has_img = false;
$fieldname = $this->fieldname;
// Check for posted values:
if (isset($_POST['cache_id'])
&& isset($_POST['ext'])
&& isset($_POST['version'])
&& isset($_POST['max_version'])) {
$this->cache_id = $_POST['cache_id'];
$this->ext = $_POST['ext'];
$this->version = $_POST['version'];
$this->max_version = $_POST['max_version'];
$cache_name = $this->buildCacheName($this->version);
if (file_exists($cache_name)) {
$this->has_img = true;
$this->current_img = $this->buildImagePath($this->version);
$this->cache_name = $cache_name;
return true;
}
}
// Check for uploaded values:
if (!isset($_FILES[$fieldname]))
{
return false;
}
if (!$_FILES[$fieldname]['error']
&& $_FILES[$fieldname]['size'] < $this->max_size
&& in_array($_FILES[$fieldname]['type'], $this->types)) {
if (is_uploaded_file($_FILES[$fieldname]['tmp_name'])) {
$this->has_img = true;
$name = $_FILES[$fieldname]['name'];
$this->cache_id = md5(time().rand());
$this->ext = substr($name, strrpos($name, '.') + 1);
$this->current_img = $this->buildImagePath($this->version);
move_uploaded_file($_FILES[$fieldname]['tmp_name'], $this->buildCacheName($this->version));
return true;
}
}
$this->errors[] = 'Image upload failed. It may exceed the maximum filesize of '
. $this->getFormatedSize($this->max_size) . ' or be of an unsupported type (types supported are '
. implode(', ', array_keys($this->types)). ').';
return false;
}
protected function imageCreateFromType($type, $filename)
{
if ($type == 'jpg') {
$type = 'jpeg';
}
$f = 'imagecreatefrom' . $type;
return $f($filename);
}
protected function imageWrite($new_img, $type, $dest_file, $quality = 100)
{
switch ($type) {
case 'jpg':
$r = imagejpeg($new_img, $dest_file, $quality);
break;
case 'png':
$r = imagepng($new_img, $dest_file);
break;
case 'gif':
$r = imagegif($new_img, $dest_file);
break;
} // switch
chmod($dest_file, 0777);
return $r;
}
protected function filter($mode = '')
{
$version = $this->version;
$new_file = $this->buildCacheName(++$version);
$source = $this->imageCreateFromType($this->ext, $this->cache_name);
$ok = true;
switch ($mode) {
case 'greyscale':
imagefilter($source, IMG_FILTER_GRAYSCALE);
break;
case 'sepia':
imagefilter($source, IMG_FILTER_GRAYSCALE);
imagefilter($source, IMG_FILTER_COLORIZE, 90, 60, 40);
break;
default:
break;
} // switch
if ($this->imageWrite($source, $this->ext, $new_file)) {
$this->version = $version;
$this->max_version = $version;
$this->current_img = $this->buildImagePath($version);
} else {
$ok = false;
$this->errors[] = 'Image greyscale filter failed.';
}
imagedestroy($source);
return $ok;
}
protected function rotate($degrees = 0)
{
$version = $this->version;
$new_file = $this->buildCacheName(++$version);
$source = $this->imageCreateFromType($this->ext, $this->cache_name);
$ok = true;
$rotate = imagerotate($source, $degrees, 0);
if ($this->imageWrite($rotate, $this->ext, $new_file)) {
$this->version = $version;
$this->max_version = $version;
$this->current_img = $this->buildImagePath($version);
} else {
$ok = false;
$this->errors[] = 'Image rotate failed.';
}
imagedestroy($rotate);
return $ok;
}
public function run()
{
$has_img = $this->hasImage();
#echo "<pre>\n";var_dump($has_img);echo "</pre>\n";exit;
if ($has_img && isset($_POST['action'])) {
$action = 'action' . ucfirst($_POST['action']);
if (method_exists($this, $action)) {
return $this->$action();
}
}
}
public function getValues()
{
$return = array(
'cache_id' => $this->cache_id,
'current_img' => $this->current_img,
'errors' => $this->errors,
'ext' => $this->ext,
'has_img' => $this->has_img,
'max_version' => $this->max_version,
'saved' => $this->saved,
'version' => $this->version
);
return $return;
}
public function actionUndo()
{
if ($this->version > 1) {
$this->version--;
$this->current_img = $this->buildImagePath($this->version);
return true;
}
return false;
}
public function actionRedo()
{
if ($this->version < $this->max_version) {
$this->version++;
$this->current_img = $this->buildImagePath($this->version);
return true;
}
return false;
}
public function actionAnticlockwise()
{
$this->rotate(90);
}
public function actionClockwise()
{
$this->rotate(-90);
}
public function actionCrop()
{
$version = $this->version;
$new_file = $this->buildCacheName(++$version);
$canvas = imagecreatetruecolor($_POST['w'], $_POST['h']);
$piece = $this->imageCreateFromType($this->ext, $this->cache_name);
$ok = true;
imagecopyresampled(
$canvas, $piece,
0, 0, $_POST['x'], $_POST['y'],
$_POST['w'], $_POST['h'], $_POST['w'], $_POST['h']
);
if ($this->imageWrite($canvas, $this->ext, $new_file)) {
$this->version = $version;
$this->max_version = $version;
$this->current_img = $this->buildImagePath($version);
} else {
$ok = false;
$this->errors[] = 'Image crop failed.';
}
imagedestroy($canvas);
imagedestroy($piece);
return $ok;
}
public function actionGreyscale()
{
return $this->filter('greyscale');
}
public function actionSepia()
{
return $this->filter('sepia');
}
public function actionSave()
{
if (!$this->savepath) {
// Clearing the cache means that the final image can't be displayed,
// so not sure what to do about this yet.
#$this->clearCache();
$this->saved = true;
return true;
}
if (!$this->savepath) {
$this->errors[] = 'No save path was set in config.';
return false;
}
$savename = $this->savename
? $this->savename
: $this->cache_id;
$savepath = trim(rtrim($this->savepath, '/')) . '/';
if (!file_exists($savepath) || !is_dir($savepath)) {
$this->errors[] = "Folder $savepath does not exist.";
return false;
}
if (!is_writable($savepath)) {
$this->errors[] = "Folder $savepath is not writable. Folder permissions: " . substr(sprintf('%o', fileperms($savepath)), -4) . '.';
return false;
}
$new_filename = $savepath . $savename . '.' . $this->ext;
if (!copy($this->cache_name, $new_filename)) {
$this->errors[] = "Failed to copy $new_filename from $this->cache_name.";
return false;
}
$this->saved = true;
return true;
}
public function getFormatedSize($bytes) {
if(!empty($bytes)) {
//SET TEXT TITLES TO SHOW AT EACH LEVEL
$s = array('bytes', 'kb', 'MB', 'GB', 'TB', 'PB');
$e = floor(log($bytes)/log(1024));
//CREATE COMPLETED OUTPUT
$output = sprintf('%.' . ($e == 0 ? 0 : 2) . 'f ' . $s[$e], ($bytes/pow(1024, $e)));
return $output;
}
}
}