-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage_editor.php
397 lines (351 loc) · 16.1 KB
/
image_editor.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
declare(strict_types=1);
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* wg Image Editor for xoops
*
* @copyright xoops
* @license GPL 2.0 or later
* @package general
* @author Wedega - Email:<[email protected]> - Website:<https://wedega.com>
* @version 1.1 image_editor.php XOOPS Project (www.xoops.org) $
*/
use Xmf\Request;
use XoopsModules\Wgevents;
use XoopsModules\Wgevents\{
Constants,
Utility,
Common\Resizer
};
include __DIR__ . '/header.php';
$GLOBALS['xoopsOption']['template_main'] = 'wgevents_image_editor.tpl';
require_once \XOOPS_ROOT_PATH . '/header.php';
$utility = new \XoopsModules\Wgevents\Utility();
$op = Request::getString('op', 'list');
$sourceId = Request::getInt('id', Request::getInt('imageIdCrop'));
$origin = Request::getString('imageOrigin');
$start = Request::getInt('start');
$limit = Request::getInt('limit', $helper->getConfig('userpager'));
$img_resize = Request::getInt('img_resize');
$uid = $xoopsUser instanceof \XoopsUser ? $xoopsUser->id() : 0;
// get all objects/classes/vars needed for image editor
$imageClass = 0; //identifier for different image classes; currently not used
$imgCurrent = [];
//if ($imageClass === Constants::IMAGECLASS_MEMBER) {
$imageId = $sourceId;
$imageHandler = $eventHandler;
$imageObj = $eventHandler->get($sourceId);
$imageOrigin = 'id';
$imgName = \mb_substr(\str_replace(' ', '', $imageObj->getVar('name')), 0, 20) . '.jpg';
$imageDir = '/uploads/wgevents/events/logos/' . $imageObj->getVar('submitter') . '/';
$imgPath = \XOOPS_ROOT_PATH . $imageDir;
$imgUrl = \XOOPS_URL . $imageDir;
$imgFinal = $imgPath . $imgName;
$imgTemp = \WGEVENTS_UPLOAD_PATH . '/temp/' . $imgName;
$redir = 'event.php?op=list&start=' . $start . '&limit=' . $limit;
$nameObj = 'title';
$fieldObj = 'logo';
$submObj = 'submitter';
/*
} else {
// only used if image editor is used for different source
// TODO: maybe for categories
}
*/
$imgCurrent['img_name'] = $imageObj->getVar($fieldObj);
$imgCurrent['src'] = $imgUrl . $imageObj->getVar($fieldObj);
$imgCurrent['origin'] = $imageClass;
$images = [];
$image_array = \XoopsLists::getImgListAsArray($imgPath);
$i = 0;
foreach ($image_array as $image_img) {
if ('blank.gif' !== $image_img) {
$i++;
$images[$i]['id'] = 'imageSelect'.$i;
$images[$i]['name'] = $image_img;
$images[$i]['title'] = $image_img;
$images[$i]['origin'] = 0; //identifier for different image classes; currently not used
if ($imgCurrent['img_name'] === $image_img) {
$images[$i]['selected'] = 1;
}
$images[$i]['src'] = $imgUrl . $image_img;
}
}
// var_dump($images);
$GLOBALS['xoopsTpl']->assign('images', $images);
unset($images);
// end: get all objects/classes/vars needed for image editor
// Define Stylesheet
$GLOBALS['xoTheme']->addStylesheet(\WGEVENTS_URL . '/assets/css/style.css');
$GLOBALS['xoTheme']->addStylesheet(\WGEVENTS_URL . '/assets/css/cropper/imageeditor.css');
// add scripts
//$GLOBALS['xoTheme']->addScript(\XOOPS_URL . '/modules/wgevents/assets/js/admin.js');
// assign vars
$GLOBALS['xoopsTpl']->assign('wgevents_url', \WGEVENTS_URL);
$GLOBALS['xoopsTpl']->assign('wgevents_icon_url_16', \WGEVENTS_ICONS_URL . '/16');
$GLOBALS['xoopsTpl']->assign('wgevents_icon_url_32', \WGEVENTS_ICONS_URL . '/32');
$GLOBALS['xoopsTpl']->assign('wgevents_upload_url', \WGEVENTS_UPLOAD_URL);
$GLOBALS['xoopsTpl']->assign('wgevents_upload_path', \WGEVENTS_UPLOAD_PATH);
$GLOBALS['xoopsTpl']->assign('wgevents_image_editor', \WGEVENTS_URL);
$GLOBALS['xoopsTpl']->assign('wgevents_upload_image_url', $imgUrl);
$GLOBALS['xoopsTpl']->assign('gridtarget', $imgName);
$GLOBALS['xoopsTpl']->assign('imgCurrent', $imgCurrent);
$GLOBALS['xoopsTpl']->assign('imageId', $imageId);
$GLOBALS['xoopsTpl']->assign('imageOrigin', $imageOrigin);
$GLOBALS['xoopsTpl']->assign('start', $start);
$GLOBALS['xoopsTpl']->assign('limit', $limit);
// Breadcrumbs
$xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_INDEX, 'link' => 'index.php'];
$xoBreadcrumbs[] = ['title' => $imageObj->getVar('name'), 'link' => 'event.php?op=show&id=' . $imageObj->getVar('id')];
$xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_IMG_EDITOR];
// get config for images
$maxwidth = $helper->getConfig('maxwidth_image');
$maxheight = $helper->getConfig('maxheight_image');
$maxsize = $helper->getConfig('maxsize_image');
$mimetypes = $helper->getConfig('mimetypes_image');
switch ($op) {
case 'creategrid':
// create an image grid based on given sources
$type = Request::getInt('type', 4);
$src[1] = Request::getString('src1');
$src[2] = Request::getString('src2');
$src[3] = Request::getString('src3');
$src[4] = Request::getString('src4');
$src[5] = Request::getString('src5');
$src[6] = Request::getString('src6');
$target = Request::getString('target');
$images = [];
for ($i = 1; $i <= 6; $i++) {
if ('' !== $src[$i]) {
$file = \str_replace(\XOOPS_URL, \XOOPS_ROOT_PATH, $src[$i]);
$images[$i] = ['file' => $file, 'mimetype' => \mime_content_type($file)];
}
}
// create basic image
$tmp = \imagecreatetruecolor($maxwidth, $maxheight);
$imgBg = imagecolorallocate($tmp, 0, 0, 0);
imagefilledrectangle($tmp, 0, 0, $maxwidth, $maxheight, $imgBg);
$final = XOOPS_UPLOAD_PATH . '/wgevents/temp/' . $target;
\unlink($final);
\imagejpeg($tmp, $final);
\imagedestroy($tmp);
$imgTemp = XOOPS_UPLOAD_PATH . '/wgevents/temp/' . $uid . 'imgTemp';
$imgHandler = new Resizer();
if (4 === $type) {
for ($i = 1; $i <= 4; $i++) {
\unlink($imgTemp . $i . '.jpg');
$imgHandler->sourceFile = $images[$i]['file'];
$imgHandler->endFile = $imgTemp . $i . '.jpg';
$imgHandler->imageMimetype = $images[$i]['mimetype'];
$imgHandler->maxWidth = (int)\round($maxwidth / 2 - 1);
$imgHandler->maxHeight = (int)\round($maxheight / 2 - 1);
$imgHandler->jpgQuality = 90;
$imgHandler->resizeAndCrop();
}
$imgHandler->mergeType = 4;
$imgHandler->endFile = $final;
$imgHandler->maxWidth = $maxwidth;
$imgHandler->maxHeight = $maxheight;
for ($i = 1; $i <= 4; $i++) {
$imgHandler->sourceFile = $imgTemp . $i . '.jpg';
$imgHandler->mergePos = $i;
$imgHandler->mergeImage();
\unlink($imgTemp . $i . '.jpg');
}
}
if (6 === $type) {
for ($i = 1; $i <= 6; $i++) {
$imgHandler->sourceFile = $images[$i]['file'];
$imgHandler->endFile = $imgTemp . $i . '.jpg';
$imgHandler->imageMimetype = $images[$i]['mimetype'];
$imgHandler->maxWidth = (int)\round($maxwidth / 3 - 1);
$imgHandler->maxHeight = (int)\round($maxheight / 2 - 1);
$imgHandler->resizeAndCrop();
}
$imgHandler->mergeType = 6;
$imgHandler->endFile = $final;
$imgHandler->maxWidth = $maxwidth;
$imgHandler->maxHeight = $maxheight;
for ($i = 1; $i <= 6; $i++) {
$imgHandler->sourceFile = $imgTemp . $i . '.jpg';
$imgHandler->mergePos = $i;
$imgHandler->mergeImage();
\unlink($imgTemp . $i . '.jpg');
}
}
break;
case 'cropimage':
// save base64_image and resize to maxwidth/maxheight
$base64_image_content = Request::getString('croppedImage');
if (\preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
$type = $result[2];
\file_put_contents($imgTemp, base64_decode(\str_replace($result[1], '', $base64_image_content), true));
}
$imgHandler = new Resizer();
$imgHandler->sourceFile = $imgTemp;
$imgHandler->endFile = $imgTemp;
$imgHandler->imageMimetype = 'image/jpeg';
$imgHandler->maxWidth = $maxwidth;
$imgHandler->maxHeight = $maxheight;
$ret = $imgHandler->resizeImage();
//\unlink($imgFinal);
break;
case 'saveImageSelected':
// save image selected from list of available images in upload folder
// Set Vars
$image_id = Request::getString('image_id');
// remove '_image' from id
$image_id = \substr($image_id, 0, -6);
$imageObj->setVar($fieldObj, $image_id);
//$imageObj->setVar($submObj, $uid); // do not change submitter of event
// Insert Data
if ($imageHandler->insert($imageObj)) {
\redirect_header($redir, 2, _MA_WGEVENTS_FORM_OK);
}
$GLOBALS['xoopsTpl']->assign('error', $imageObj->getHtmlErrors());
break;
case 'saveGrid':
// save before created grid image
$imgTempGrid = Request::getString('gridImgFinal');
$ret = \rename($imgTempGrid, $imgFinal);
// Set Vars
$imageObj->setVar($fieldObj, $imgName);
//$imageObj->setVar($submObj, $uid); // do not change submitter of event
// Insert Data
if ($imageHandler->insert($imageObj)) {
\redirect_header($redir, 2, _MA_WGEVENTS_FORM_OK);
}
$GLOBALS['xoopsTpl']->assign('error', $imageObj->getHtmlErrors());
break;
case 'saveCrop':
// save before created cropped image
\unlink($imgFinal);
$ret = \rename($imgTemp, $imgFinal);
// Set Vars
$imageObj->setVar($fieldObj, $imgName);
//$imageObj->setVar($submObj, $uid); // do not change submitter of event
// Insert Data
if ($imageHandler->insert($imageObj, true)) {
\redirect_header($redir, 2, _MA_WGEVENTS_FORM_OK);
}
$GLOBALS['xoopsTpl']->assign('error', $imageObj->getHtmlErrors());
break;
case 'uploadImage':
// Security Check
if (!$GLOBALS['xoopsSecurity']->check()) {
\redirect_header($redir, 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
}
// Set Vars
require_once \XOOPS_ROOT_PATH . '/class/uploader.php';
$fileName = $_FILES['attachedfile']['name'];
$imageMimetype = $_FILES['attachedfile']['type'];
$uploaderErrors = '';
$uploader = new \XoopsMediaUploader($imgPath, $mimetypes, $maxsize, null, null);
if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
$extension = \preg_replace('/^.+\.([^.]+)$/sU', '', $fileName);
$imgName .= '.' . $extension;
$uploader->setPrefix($imgName);
$uploader->fetchMedia($_POST['xoops_upload_file'][0]);
if (!$uploader->upload()) {
$uploaderErrors = $uploader->getErrors();
} else {
$savedFilename = $uploader->getSavedFileName();
$imageObj->setVar($fieldObj, $savedFilename);
// resize image
if (1 == $img_resize) {
$imgHandler = new Resizer();
$imgHandler->sourceFile = $imgPath . $savedFilename;
$imgHandler->endFile = $imgPath . $savedFilename;
$imgHandler->imageMimetype = $imageMimetype;
$imgHandler->maxWidth = $maxwidth;
$imgHandler->maxHeight = $maxheight;
$result = $imgHandler->resizeImage();
}
$imageObj->setVar($fieldObj, $savedFilename);
//$imageObj->setVar($submObj, $uid); // do not change submitter of event
}
} elseif ($fileName > '') {
$uploaderErrors = $uploader->getErrors();
}
if ('' !== $uploaderErrors) {
\redirect_header($redir, 5, $uploaderErrors);
}
// Insert Data
if ($imageHandler->insert($imageObj)) {
\redirect_header($redir, 2, _MA_WGEVENTS_FORM_OK);
}
// Get Form
$GLOBALS['xoopsTpl']->assign('error', $imageObj->getHtmlErrors());
$form = $imageObj->getFormUploadImage($imageOrigin, $imageId);
$GLOBALS['xoopsTpl']->assign('form', $form->render());
break;
case 'imghandler':
default:
$GLOBALS['xoTheme']->addStylesheet(\WGEVENTS_URL . '/assets/css/cropper/cropper.min.css');
$GLOBALS['xoTheme']->addScript(\WGEVENTS_URL . '/assets/js/cropper/cropper.min.js');
$GLOBALS['xoTheme']->addScript(\WGEVENTS_URL . '/assets/js/cropper/cropper-main.js');
$GLOBALS['xoopsTpl']->assign('nbModals', [1, 2, 3, 4, 5, 6]);
// get form for upload album image
$currImage = (string)$imageObj->getVar($fieldObj);
if ('' === $currImage) {
$currImage = 'blank.gif';
}
$image_path = $imgPath . $currImage;
$width = 0;
$height= 0;
if (\file_exists($image_path)) {
// get size of current album image
list($width, $height, $type, $attr) = \getimagesize($image_path);
}
$GLOBALS['xoopsTpl']->assign('image_path', $image_path);
$GLOBALS['xoopsTpl']->assign('albimage_width', $width);
$GLOBALS['xoopsTpl']->assign('albimage_height', $height);
$form = getFormUploadImage($imageOrigin, $imageId);
$GLOBALS['xoopsTpl']->assign('form_uploadimage', $form->render());
$GLOBALS['xoopsTpl']->assign('btn_style', 'btn-primary');
break;
}
$GLOBALS['xoopsTpl']->assign('panel_type', $helper->getConfig('panel_type'));
include __DIR__ . '/footer.php';
/**
* @public function getFormUploadImage:
* provide form for uploading a new image
* @param $imageOrigin
* @param $imageId
* @return \XoopsThemeForm
*/
function getFormUploadImage($imageOrigin, $imageId)
{
$helper = \XoopsModules\Wgevents\Helper::getInstance();
// Get Theme Form
\xoops_load('XoopsFormLoader');
$form = new \XoopsThemeForm('', 'formuploadimage', 'image_editor.php', 'post', true);
$form->setExtra('enctype="multipart/form-data"');
// upload new image
$imageTray1 = new \XoopsFormElementTray(\_MA_WGEVENTS_FORM_UPLOAD_IMG, '<br>');
$imageFileSelect = new \XoopsFormFile('', 'attachedfile', $helper->getConfig('maxsize'));
$imageTray1->addElement($imageFileSelect);
$form->addElement($imageTray1);
$cond = \_MA_WGEVENTS_IMG_MAXSIZE . ': ' . ($helper->getConfig('maxsize_image') / 1048576) . ' ' . \_MA_WGEVENTS_SIZE_MB . '<br>';
//$cond .= \_MI_WGEVENTS_MAXWIDTH . ': ' . $helper->getConfig('maxwidth') . ' px<br>';
//$cond .= \_MI_WGEVENTS_MAXHEIGHT . ': ' . $helper->getConfig('maxheight') . ' px<br>';
$cond .= \_MA_WGEVENTS_IMG_MIMETYPES . ': ' . \implode(', ', $helper->getConfig('mimetypes_image')) . '<br>';
$form->addElement(new \XoopsFormLabel(\_MA_WGEVENTS_IMG_EDITOR_UPLOAD, $cond));
$imageTray3 = new \XoopsFormElementTray(\_MA_WGEVENTS_IMG_EDITOR_RESIZE, '');
$resizeinfo = \str_replace('%w', (string)$helper->getConfig('maxwidth_image'), \_MA_WGEVENTS_IMG_EDITOR_RESIZE_DESC);
$resizeinfo = \str_replace('%h', (string)$helper->getConfig('maxheight_image'), $resizeinfo);
$imageTray3->addElement(new \XoopsFormLabel($resizeinfo, ''));
$imageTray3->addElement(new \XoopsFormRadioYN('', 'img_resize', 1));
$form->addElement($imageTray3);
$form->addElement(new \XoopsFormHidden($imageOrigin, $imageId));
$form->addElement(new \XoopsFormHidden('op', 'uploadImage'));
$form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', '', false));
return $form;
}