Skip to content

Commit adbe213

Browse files
committed
Automatic image resize before upload with ElFinder - refs BT#22708
1 parent 63aafbc commit adbe213

File tree

5 files changed

+84
-33
lines changed

5 files changed

+84
-33
lines changed

main/inc/ajax/document.ajax.php

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -226,40 +226,20 @@
226226
exit;
227227
}
228228

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-
}
229+
try {
230+
$fileUpload['size'] = DocumentManager::autoResizeImageIfNeeded(
231+
$fileUpload['size'],
232+
$fileUpload['tmp_name']
233+
);
234+
} catch (Exception $e) {
235+
echo json_encode([
236+
'uploaded' => 0,
237+
'error' => [
238+
'message' => $e->getMessage(),
239+
]
240+
]);
257241

258-
if ($thumbSize) {
259-
$temp->resize($thumbSize);
260-
$temp->send_image($fileUpload['tmp_name']);
261-
$fileUpload['size'] = filesize($fileUpload['tmp_name']);
262-
}
242+
exit;
263243
}
264244

265245
$isAllowedToEdit = api_is_allowed_to_edit(null, true);

main/inc/lib/document.lib.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7661,4 +7661,39 @@ private static function includeMathJaxScript($content)
76617661

76627662
return $content;
76637663
}
7664+
7665+
public static function autoResizeImageIfNeeded(int $size, string $tmpName): int
7666+
{
7667+
$resizeMax = api_get_configuration_value('wysiwyg_image_auto_resize_max');
7668+
7669+
if (is_array($resizeMax)) {
7670+
if ($size > ($resizeMax['mb'] * 1024 * 1024)) {
7671+
throw new Exception(get_lang('UplFileTooBig'));
7672+
}
7673+
7674+
$temp = new Image($tmpName);
7675+
$pictureInfo = $temp->get_image_info();
7676+
7677+
$thumbSize = 0;
7678+
7679+
if ($pictureInfo['width'] > $pictureInfo['height']) {
7680+
if ($pictureInfo['width'] > $resizeMax['w']) {
7681+
$thumbSize = $resizeMax['w'];
7682+
}
7683+
} else {
7684+
if ($pictureInfo['height'] > $resizeMax['h']) {
7685+
$thumbSize = $resizeMax['h'];
7686+
}
7687+
}
7688+
7689+
if ($thumbSize) {
7690+
$temp->resize($thumbSize);
7691+
$temp->send_image($tmpName);
7692+
7693+
return filesize($tmpName);
7694+
}
7695+
}
7696+
7697+
return $size;
7698+
}
76647699
}

src/Chamilo/CoreBundle/Component/Editor/Driver/CourseDriver.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ public function upload($fp, $dst, $name, $tmpname, $hashes = [])
283283
return false;
284284
}
285285

286+
if (str_starts_with($this->mimetype($tmpname, $name), 'image')) {
287+
try {
288+
\DocumentManager::autoResizeImageIfNeeded($size, $tmpname);
289+
} catch (\Exception $e) {
290+
$this->addError($e->getMessage());
291+
292+
return false;
293+
}
294+
}
295+
286296
$result = parent::upload($fp, $dst, $name, $tmpname);
287297
$name = $result['name'];
288298
$filtered = \URLify::filter($result['name'], 80, '', true);

src/Chamilo/CoreBundle/Component/Editor/Driver/HomeDriver.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ public function upload($fp, $dst, $name, $tmpname)
4747
$this->setConnectorFromPlugin();
4848

4949
if ($this->allow()) {
50+
if (str_starts_with($this->mimetype($tmpname, $name), 'image')) {
51+
try {
52+
\DocumentManager::autoResizeImageIfNeeded(
53+
filesize($tmpname),
54+
$tmpname
55+
);
56+
} catch (\Exception $e) {
57+
$this->addError($e->getMessage());
58+
59+
return false;
60+
}
61+
}
62+
5063
return parent::upload($fp, $dst, $name, $tmpname);
5164
}
5265
}

src/Chamilo/CoreBundle/Component/Editor/Driver/PersonalDriver.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ public function upload($fp, $dst, $name, $tmpname, $hashes = [])
120120
{
121121
$this->setConnectorFromPlugin();
122122
if ($this->allow()) {
123+
if (str_starts_with($this->mimetype($tmpname, $name), 'image')) {
124+
try {
125+
\DocumentManager::autoResizeImageIfNeeded(
126+
filesize($tmpname),
127+
$tmpname
128+
);
129+
} catch (\Exception $e) {
130+
$this->addError($e->getMessage());
131+
132+
return false;
133+
}
134+
}
135+
123136
return parent::upload($fp, $dst, $name, $tmpname);
124137
}
125138
}

0 commit comments

Comments
 (0)