Skip to content

Commit d5e64a5

Browse files
committed
Add wysiwyg_image_auto_resize_max conf setting - refs BT#22708
Automatic image resize before upload image with CKEditor
1 parent 6d99fd9 commit d5e64a5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

main/inc/ajax/document.ajax.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
use Chamilo\CoreBundle\Component\Editor\Driver\Driver;
9+
use Chamilo\CoreBundle\Component\Editor\Driver\PersonalDriver;
910

1011
require_once __DIR__.'/../global.inc.php';
1112

@@ -225,6 +226,42 @@
225226
exit;
226227
}
227228

229+
$resizeMax = api_get_configuration_value('wysiwyg_image_auto_resize_max');
230+
231+
if (is_array($resizeMax)) {
232+
if ($fileUpload['size'] > ($resizeMax['mb'] * 1024 * 1024)) {
233+
echo json_encode([
234+
'uploaded' => 0,
235+
'error' => [
236+
'message' => get_lang('UplFileTooBig'),
237+
]
238+
]);
239+
240+
exit;
241+
}
242+
243+
$temp = new Image($fileUpload['tmp_name']);
244+
$pictureInfo = $temp->get_image_info();
245+
246+
$thumbSize = 0;
247+
248+
if ($pictureInfo['width'] > $pictureInfo['height']) {
249+
if ($pictureInfo['width'] > $resizeMax['w']) {
250+
$thumbSize = $resizeMax['w'];
251+
}
252+
} else {
253+
if ($pictureInfo['height'] > $resizeMax['h']) {
254+
$thumbSize = $resizeMax['h'];
255+
}
256+
}
257+
258+
if ($thumbSize) {
259+
$temp->resize($thumbSize);
260+
$temp->send_image($fileUpload['tmp_name']);
261+
$fileUpload['size'] = filesize($fileUpload['tmp_name']);
262+
}
263+
}
264+
228265
$isAllowedToEdit = api_is_allowed_to_edit(null, true);
229266
if ($isAllowedToEdit) {
230267
$globalFile = ['files' => $fileUpload];

main/install/configuration.dist.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,6 +2296,13 @@
22962296
// Enable image upload as file when doing a copy in the content or a drag and drop.
22972297
//$_configuration['enable_uploadimage_editor'] = false;
22982298

2299+
// Automatic image resize before upload image with CKEditor
2300+
/*$_configuration['wysiwyg_image_auto_resize_max'] = [
2301+
'w'=> 800, //max width
2302+
'h' => 600, //max height
2303+
'mb' => 2 //max size (in MB)
2304+
];*/
2305+
22992306
// Ckeditor settings.
23002307
//$_configuration['editor_settings'] = ['config' => ['youtube_responsive' => true, 'image_responsive' => true]];
23012308

0 commit comments

Comments
 (0)