-
Notifications
You must be signed in to change notification settings - Fork 15
/
htmlEditorImageUpload.php
329 lines (285 loc) · 9.8 KB
/
htmlEditorImageUpload.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
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
<?php
require_once 'thumbnailer/ThumbLib.inc.php';
// change these parameters to fit your server config
$allowedFormats = "jpg,jpeg,gif,png"; // Allowed image formats
$maxSize = "102400"; // Max image size. 10485760 = 10MB
$imagesPath = "uploads/"; // path where the files will be uploaded to the server
$imagesThumbsPath = "uploads/thumbs/"; // path where the image thumbs will be uploaded to the server
$imagesTumbsUrl = "http://www.racho.es/examples/HtmlEditorImageUpload/uploads/thumbs/";
$imagesUrl = "http://www.racho.es/examples/HtmlEditorImageUpload/uploads/"; // url to the images
//$imagesTumbsUrl = "http://localhost/HtmlEditorImageUpload/uploads/thumbs/";
//$imagesUrl = "http://localhost/HtmlEditorImageUpload/uploads/"; // url to the images
$createThumbnails = true;
if(isset($_REQUEST['action']))
{
switch($_REQUEST['action'])
{
case 'upload':
print_r( json_encode( uploadHtmlEditorImage($allowedFormats,$maxSize,$imagesPath,$imagesUrl,$createThumbnails,$imagesTumbsUrl,$imagesThumbsPath) ) );
break;
case 'crop':
$imageSrc = isset($_REQUEST["image"])?($_REQUEST["image"]):'';
$width = isset($_REQUEST["width"])?intval($_REQUEST["width"]):0;
$height = isset($_REQUEST["height"])?intval($_REQUEST["height"]):0;
$offsetLeft = isset($_REQUEST["offsetLeft"])?intval($_REQUEST["offsetLeft"]):0;
$offsetTop = isset($_REQUEST["offsetTop"])?intval($_REQUEST["offsetTop"]):0;
$zoom = isset($_REQUEST["zoom"])?intval($_REQUEST["zoom"]):1;
print_r( json_encode( cropImage($imagesPath,$imagesThumbsPath,$imagesUrl,$imagesTumbsUrl,$imageSrc,$width,$height,$offsetLeft,$offsetTop, $zoom, $allowedFormats) ) );
break;
case 'rotate':
$imageSrc = isset($_REQUEST["image"])?($_REQUEST["image"]):'';
print_r( json_encode( rotateImage($imagesPath,$imagesThumbsPath,$imagesUrl,$imageSrc) ) );
break;
case 'resize':
$width = isset($_REQUEST["width"])?intval($_REQUEST["width"]):0;
$height = isset($_REQUEST["height"])?intval($_REQUEST["height"]):0;
$imageSrc = isset($_REQUEST["image"])?($_REQUEST["image"]):'';
print_r( json_encode( resizeImage($imagesPath,$imagesThumbsPath,$imagesUrl,$imageSrc,$width,$height) ) );
break;
case 'imagesList':
$limit = isset($_REQUEST["limit"])?intval($_REQUEST["limit"]):10;
$start = isset($_REQUEST["start"])?intval($_REQUEST["start"]):0;
$query = isset($_REQUEST["query"])?$_REQUEST["query"]:0;
print_r(json_encode( getImages($imagesPath, $imagesUrl,$imagesTumbsUrl,$imagesThumbsPath, $allowedFormats, $start, $limit, $query) ));
break;
case 'delete':
$image = isset($_REQUEST["image"]) ? stripslashes($_REQUEST["image"]):"";
print_r( json_encode( deleteImage($imagesPath,$imagesThumbsPath, $image) ));
break;
}
}
function checkAllowedFormats($imageName, $allowedFormats)
{
// quitamos caracteres extraños
$imageName = preg_replace('/[^(\x20-\x7F)]*/','', $imageName);
// extensión del archivo
$ext = strtolower( substr($imageName, strpos($imageName,'.')+1, strlen($imageName)-1) );
if(!in_array($ext,explode(',', $allowedFormats))) return false;
else return true;
}
function uploadHtmlEditorImage($allowedFormats,$maxSize,$imagesPath,$imagesUrl,$createThumbnails=false,$imagesTumbsUrl,$imagesThumbsPath)
{
global $_FILES;
$result = array();
if(!checkAllowedFormats($_FILES['photo-path']['name'], $allowedFormats))
{
$result= array(
'success' => false,
'message' => 'Error',
'data' => '',
'total' => '0',
'errors' => 'The file you attempted to upload is not allowed.'
);
return $result;
}
$nombreArchivo = preg_replace('/[^(\x20-\x7F)]*/','', $_FILES['photo-path']['name']);
$ext = strtolower( substr($nombreArchivo, strpos($nombreArchivo,'.')+1, strlen($nombreArchivo)-1) );
while (file_exists($imagesPath.$nombreArchivo)) {
$prefijo = substr(md5(uniqid(rand())),0,6);
$nombreArchivo = $prefijo.'_'.$nombreArchivo;
}
if(filesize($_FILES['photo-path']['tmp_name']) > $maxSize)
{
$result= array(
'success' => false,
'message' => 'Error',
'data' => '',
'total' => '0',
'errors' => 'The file you attempted to upload is too large.'
);
return $result;
}
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($imagesPath))
{
$result= array(
'success' => false,
'message' => 'Error',
'data' => '',
'total' => '0',
'errors' => 'You cannot upload to the specified directory: '.$imagesPath.', please CHMOD it to 777 or check permissions'
);
return $result;
}
if(move_uploaded_file($_FILES['photo-path']['tmp_name'],$imagesPath.$nombreArchivo))
{
if($createThumbnails)
{
$thumb = PhpThumbFactory::create($imagesPath.$nombreArchivo);
$thumb->adaptiveResize(64, 64);
$thumb->save($imagesThumbsPath.$nombreArchivo);
}
$result= array(
'success' => true,
'message' => 'Image Uploaded Successfully',
'data' => array('src'=>$imagesUrl.$nombreArchivo),
'total' => '1',
'errors' => ''
);
}
else
{
$result= array(
'success' => false,
'message' => 'Error',
'data' => '',
'total' => '0',
'errors' => 'Error Uploading Image'
);
}
return $result;
}
function deleteImage($imagesPath,$imagesThumbsPath, $image = null)
{
if(file_exists($imagesPath.$image))
{
// remove image
unlink($imagesPath.$image);
// remove thumbnail if exists
if(file_exists($imagesThumbsPath.$image))unlink($imagesThumbsPath.$image);
return array(
'success' => true,
'message' => 'Success',
'data' => '',
'total' => 1,
'errors' => ''
);
}
else
{
return array(
'success' => false,
'message' => 'Error',
'data' => '',
'total' => 0,
'errors' => 'Delete Operation Failed'
);
}
}
function cropImage($imagesPath,$imagesThumbsPath,$imagesUrl,$imagesThumbsUrl,$imageSrc,$width,$height,$offsetLeft,$offsetTop, $zoom, $allowedFormats)
{
$imageName = preg_replace('/[^(\x20-\x7F)]*/','', basename($imageSrc));
$zoom = $zoom/100;
$left = $offsetLeft > 0 ? round($offsetLeft/$zoom) : 0;
$width = round($width/$zoom);
$top = $offsetTop > 0 ? round($offsetTop/$zoom) : 0;
$height = round($height/$zoom);
try
{
// make the cropped image
$thumb = PhpThumbFactory::create($imagesPath.$imageName);
$thumb->crop($left, $top, $width, $height);
$thumb->save($imagesPath.$imageName);
// make the thumbnail for the cropped image
$thumb->adaptiveResize(64, 64);
$thumb->save($imagesThumbsPath.$imageName);
return array(
'success' => true,
'message' => 'Success',
'data' => array('src'=>$imagesUrl.$imageName),
'total' => 1,
'errors' => ''
);
}
catch (Exception $e)
{
return array(
'success' => false,
'message' => 'Error',
'data' => 'Error with rotate operation.'.$e,
'total' => 1,
'errors' => ''
);
}
}
function rotateImage($imagesPath,$imagesThumbsPath,$imagesUrl,$imageSrc)
{
$imageName = preg_replace('/[^(\x20-\x7F)]*/','', basename($imageSrc));
try
{
// make the cropped image
$thumb = PhpThumbFactory::create($imagesPath.$imageName);
$thumb->rotateImageNDegrees(-90);
$thumb->save($imagesPath.$imageName);
// make the thumbnail for the cropped image
$thumb->adaptiveResize(64, 64);
$thumb->save($imagesThumbsPath.$imageName);
return array(
'success' => true,
'message' => 'Success',
'data' => array('src'=>$imagesUrl.$imageName),
'total' => 1,
'errors' => ''
);
}
catch (Exception $e)
{
return array(
'success' => false,
'message' => 'Error',
'data' => 'Error with rotate operation.'.$e,
'total' => 1,
'errors' => ''
);
}
}
function resizeImage($imagesPath,$imagesThumbsPath,$imagesUrl,$imageSrc, $width, $height)
{
$imageName = preg_replace('/[^(\x20-\x7F)]*/','', basename($imageSrc));
try
{
$thumb = PhpThumbFactory::create($imagesPath.$imageName, array('resizeUp' => true));
$thumb->resize($width, $height);
$thumb->save($imagesPath.$imageName);
// make the thumbnail for the cropped image
$thumb->adaptiveResize(64, 64);
$thumb->save($imagesThumbsPath.$imageName);
return array(
'success' => true,
'message' => 'Success',
'data' => array('src'=>$imagesUrl.$imageName),
'total' => 1,
'errors' => ''
);
}
catch (Exception $e)
{
return array(
'success' => false,
'message' => 'Error',
'data' => 'Error with rotate operation.'.$e,
'total' => 1,
'errors' => ''
);
}
}
function getImages($imagesPath, $imagesUrl,$imagesTumbsUrl,$imagesThumbsPath, $allowedFormats, $start = 0, $limit = 10,$query ="")
{
// array to hold return value
$results = array();
$handler = opendir($imagesPath);
// open directory and walk through the filenames
while ($file = readdir($handler)) {
// extensión del archivo
$ext = strtolower( substr($file, strpos($file,'.')+1, strlen($file)-1) );
if(in_array($ext,explode(',', $allowedFormats)))
{
$resume = strlen ( $file ) > 18 ? substr($file,0, 12).'...' : $file;
if ($file != "." && $file != "..") {
if( $query == "" || ($query != "" && stripos($file,$query)!== false) ){
$thumbSrc = file_exists($imagesThumbsPath.$file) ? $imagesTumbsUrl.$file : $imagesUrl.$file;
$thumbSrc = strpos($thumbSrc, "https://") ? $thumbSrc : $thumbSrc.'?'.rand(1, 10000);
$results[] = array('fullname'=>$file,'name'=>$resume,'src'=>$imagesUrl.$file,'thumbSrc'=>$thumbSrc);
}
}
}
}
return array(
'success' => true,
'message' => 'Success',
'data' => $output = array_slice($results, $start, $limit),
'total' => count($results),
'errors' => ''
);
}
?>