Skip to content

Commit c5f60f4

Browse files
committed
v1.4.5
- Fixed Some servers do not send the new file size after uploading images.
1 parent eebaa44 commit c5f60f4

File tree

10 files changed

+182
-54
lines changed

10 files changed

+182
-54
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ For full functionality "Maximum avatar file size" in "ACP" > "Board configuratio
5454

5555
## Changelog
5656

57+
### v1.4.5 (23-06-2024)
58+
- Fixed Some servers do not send the new file size after uploading images.
59+
5760
### v1.4.4 (19-06-2024)
5861
- Added Set max filesize in attachment extension groups on migraton to 0.
5962
- Fixed Don't work in none standard attachment extension groups

imcger/imgupload/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ For full functionality "Maximum avatar file size" in "ACP" > "Board configuratio
5454

5555
## Changelog
5656

57+
### v1.4.5 (23-06-2024)
58+
- Fixed Some servers do not send the new file size after uploading images.
59+
5760
### v1.4.4 (19-06-2024)
5861
- Added Set max filesize in attachment extension groups on migraton to 0.
5962
- Fixed Don't work in none standard attachment extension groups

imcger/imgupload/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"type": "phpbb-extension",
44
"description": "Using ImageMagick php librarie for resize image attachments and creating thumbnails.",
55
"homepage": "https://github.com/IMC-GER/phpBB-Image-upload-use-ImageMagick/tags",
6-
"version": "1.4.4",
7-
"time": "2024-06-19",
6+
"version": "1.4.5",
7+
"time": "2024-06-23",
88
"license": "GPL-2.0-only",
99
"authors": [
1010
{

imcger/imgupload/config/routing.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
imcger_imgupload_save_image_controller:
2-
path: /imgupload/imgsave
1+
imcger_imgupload_ajax_controller:
2+
path: /imgupload/{order}
33
defaults:
4-
_controller: 'imcger.imgupload.save_rotated_img_controller:save_image'
4+
_controller: 'imcger.imgupload.ajax_controller:request'

imcger/imgupload/config/services.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ services:
88
- '@request'
99
- '@ext.manager'
1010

11-
imcger.imgupload.save_rotated_img_controller:
12-
class: imcger\imgupload\controller\save_rotated_img_controller
11+
imcger.imgupload.ajax_controller:
12+
class: imcger\imgupload\controller\ajax_controller
1313
arguments:
1414
- '@config'
1515
- '@user'

imcger/imgupload/controller/save_rotated_img_controller.php renamed to imcger/imgupload/controller/ajax_controller.php

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* Ajax main controller
1515
*/
16-
class save_rotated_img_controller
16+
class ajax_controller
1717
{
1818
/** @var \phpbb\config\config */
1919
protected $config;
@@ -45,6 +45,9 @@ class save_rotated_img_controller
4545
/** @var string phpEx */
4646
protected $php_ext;
4747

48+
/** @var string Extension name */
49+
protected $ext_display_name;
50+
4851
/**
4952
* Constructor for ajax controller
5053
*
@@ -85,19 +88,18 @@ public function __construct(
8588

8689
// Add language file
8790
$this->language->add_lang('attachment', 'imcger/imgupload');
91+
92+
// Get name of the extension
93+
$metadata_manager = $this->ext_manager->create_extension_metadata_manager('imcger/imgupload');
94+
$this->ext_display_name = $metadata_manager->get_metadata('display-name');
8895
}
8996

9097
/**
91-
* Rotate Image with ImageMagick
92-
*
93-
* @var int attach_id contain attach id
94-
* @var int img_rotate_deg contain rotate degree
95-
* @var int creation_time creation time of token
96-
* @var string form_token form token
98+
* Assigns the Ajax request app.php/imgupload/{order}
9799
*
98-
* @return array Json arry with status, old and new attach id, new file size or error message
100+
* @access public
99101
*/
100-
public function save_image()
102+
public function request($order)
101103
{
102104
// No ajax request, redirect to forum index
103105
if (!$this->request->is_ajax())
@@ -111,23 +113,53 @@ public function save_image()
111113
$this->json_response(3);
112114
}
113115

114-
// Get name of the extension
115-
$metadata_manager = $this->ext_manager->create_extension_metadata_manager('imcger/imgupload');
116-
$ext_display_name = $metadata_manager->get_metadata('display-name');
117-
118116
// Check form token
119-
if (!check_form_key('posting'))
117+
if (!(check_form_key('posting') || check_form_key('ucp_pm_compose')))
120118
{
121-
$this->json_response(5, $ext_display_name, $this->language->lang('FORM_INVALID'));
119+
$this->json_response(5, $this->ext_display_name, $this->language->lang('FORM_INVALID'));
122120
}
123121

124-
// Get variable, accept only integer
125-
$img_attach_id = intval($this->request->variable('attach_id', ''));
126-
$img_rotate_deg = intval($this->request->variable('img_rotate_deg', ''));
122+
switch ($order)
123+
{
124+
// Ajax request to save image after rotation
125+
case 'save_image':
126+
// Get variable, accept only integer
127+
$img_attach_id = intval($this->request->variable('attach_id', 0));
128+
$img_rotate_deg = intval($this->request->variable('img_rotate_deg', 0));
129+
130+
$this->save_image($img_attach_id, $img_rotate_deg);
131+
break;
132+
133+
// Ajax request for image size
134+
case 'image_size':
135+
// Get variable, accept only integer
136+
$img_attach_id = intval($this->request->variable('attach_id', 0));
137+
138+
$this->image_size($img_attach_id);
139+
break;
140+
141+
// Displays the start page of phpBB
142+
default:
143+
redirect($this->phpbb_root_path . 'index.' . $this->phpEx);
144+
break;
145+
}
146+
}
127147

148+
/**
149+
* Rotate and save Image with ImageMagick
150+
*
151+
* @var int attach_id contain attach id
152+
* @var int img_rotate_deg contain rotate degree
153+
* @var int creation_time creation time of token
154+
* @var string form_token form token
155+
*
156+
* @return array Json arry with status, old and new attach id, new file size or error message
157+
*/
158+
private function save_image($img_attach_id, $img_rotate_deg)
159+
{
128160
if (!$img_attach_id || $img_rotate_deg < 1 || $img_rotate_deg > 360)
129161
{
130-
$this->json_response(5, $ext_display_name, $this->language->lang('IUL_WRONG_PARAM'));
162+
$this->json_response(5, $this->ext_display_name, $this->language->lang('IUL_WRONG_PARAM'));
131163
}
132164

133165
if ($this->auth->acl_gets('u_attach', 'a_attach', 'f_attach'))
@@ -143,7 +175,7 @@ public function save_image()
143175

144176
if (!isset($img_data) || $img_data == false)
145177
{
146-
$this->json_response(4, $ext_display_name, $this->language->lang('IUL_NO_IMG_IN_DATABASE'));
178+
$this->json_response(4, $this->ext_display_name, $this->language->lang('IUL_NO_IMG_IN_DATABASE'));
147179
}
148180

149181
// Get image file path
@@ -156,7 +188,7 @@ public function save_image()
156188
}
157189
else
158190
{
159-
$this->json_response(4, $ext_display_name, $this->language->lang('IUL_IMG_NOT_EXIST'));
191+
$this->json_response(4, $this->ext_display_name, $this->language->lang('IUL_IMG_NOT_EXIST'));
160192
}
161193

162194
if ($img_data['thumbnail'] && $this->filesystem->exists($thumb_file_path))
@@ -182,21 +214,41 @@ public function save_image()
182214
$sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int) $img_attach_id;
183215
$this->db->sql_query($sql);
184216

185-
$this->json_response(0, $ext_display_name, $alert_msg ?? '', $img_attach_id, $new_attach_id, $img_data['filesize']);
217+
$this->json_response(0, $this->ext_display_name, $alert_msg ?? '', $img_attach_id, $new_attach_id, $img_data['filesize']);
186218
}
187219
else
188220
{
189-
$this->json_response(5, $ext_display_name, $this->language->lang('IUL_DATABASE_NOT_UPDATE'));
221+
$this->json_response(5, $this->ext_display_name, $this->language->lang('IUL_DATABASE_NOT_UPDATE'));
190222
}
191223
}
192224

225+
/**
226+
* Get Image size
227+
*
228+
* @var int $attach_id contain attach id
229+
*
230+
* @return array Json arry with attach id and file size
231+
*/
232+
private function image_size($attach_id)
233+
{
234+
$sql = 'SELECT filesize
235+
FROM ' . ATTACHMENTS_TABLE . '
236+
WHERE attach_id = ' . (int) $attach_id;
237+
238+
$result = $this->db->sql_query($sql);
239+
$img_data = $this->db->sql_fetchrow($result);
240+
$this->db->sql_freeresult($result);
241+
242+
$this->json_response(0, $this->ext_display_name, '', $attach_id, $attach_id, $img_data['filesize']);
243+
}
244+
193245
/**
194246
* Rotate Image with ImageMagick
195247
*
196-
* @param string $path Path to the image file
197-
* @param int $deg Rotation angle
248+
* @param string $path Path to the image file
249+
* @param int $deg Rotation angle
198250
*
199-
* @return int Image file size
251+
* @return int $filesize Image file size
200252
*/
201253
private function rotate_image($path, $deg)
202254
{

imcger/imgupload/event/main_listener.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public function set_template_vars()
138138
'IUL_IMG_SET_INLINE' => $this->config['imcger_imgupload_image_inline'],
139139
'IUL_IMG_MAX_THUMB_WIDTH' => $img_max_thumb_width ? $img_max_thumb_width . 'px' : false,
140140
'IMGUPLOAD_TITLE' => $metadata_manager->get_metadata('display-name'),
141-
'U_IUL_SAVE_IMAGE' => $this->helper->route('imcger_imgupload_save_image_controller'),
141+
'U_IUL_SAVE_IMAGE' => $this->helper->route('imcger_imgupload_ajax_controller', ['order' => 'save_image']),
142+
'U_IUL_IMAGE_SIZE' => $this->helper->route('imcger_imgupload_ajax_controller', ['order' => 'image_size']),
142143
]);
143144
}
144145

@@ -316,7 +317,7 @@ public function imcger_modify_uploaded_file($event)
316317
*
317318
* @param \phpbb\event\data $event Event object
318319
*/
319-
function imcger_modify_uploaded_avatar($event)
320+
public function imcger_modify_uploaded_avatar($event)
320321
{
321322
if ($this->config['imcger_imgupload_avatar_resize'])
322323
{
@@ -380,7 +381,7 @@ function imcger_modify_uploaded_avatar($event)
380381
*
381382
* @param \phpbb\event\data $event Event object
382383
*/
383-
function imcger_viewtopic_modify_post_row($event)
384+
public function imcger_viewtopic_modify_post_row($event)
384385
{
385386
$row = $event['row'];
386387
$post_attachments = $event['attachments'];
@@ -422,7 +423,7 @@ function imcger_viewtopic_modify_post_row($event)
422423
*
423424
* @param \phpbb\event\data $event Event object
424425
*/
425-
function imcger_posting_modify_template_vars($event)
426+
public function imcger_posting_modify_template_vars($event)
426427
{
427428
// Get message text and attachment data
428429
$message_parser = $event['message_parser'];
@@ -458,7 +459,7 @@ function imcger_posting_modify_template_vars($event)
458459
*
459460
* @return bool $img_resize images is resize
460461
*/
461-
function resize_image($image, $max_width, $max_height)
462+
public function resize_image($image, $max_width, $max_height)
462463
{
463464
$img_resize = false;
464465

@@ -504,7 +505,7 @@ function resize_image($image, $max_width, $max_height)
504505
*
505506
* @return string $imageformat images format
506507
*/
507-
function set_image_format($image, $mimetype)
508+
public function set_image_format($image, $mimetype)
508509
{
509510
// Check the mimetype and set the appropriate type for the image
510511
switch ($mimetype)
@@ -541,7 +542,7 @@ function set_image_format($image, $mimetype)
541542
* @param object $image image object
542543
* @param integer $quality image quality value
543544
*/
544-
function set_image_compression($image, $quality = 80)
545+
public function set_image_compression($image, $quality = 80)
545546
{
546547
$image_format = $image->getImageFormat();
547548

@@ -581,7 +582,7 @@ function set_image_compression($image, $quality = 80)
581582
* @return array changed orientation changed
582583
* rotate rotate 90 degree
583584
*/
584-
function image_auto_rotate($image)
585+
public function image_auto_rotate($image)
585586
{
586587
$is_rotate = false; // true, when orientation change between portrait and landscape
587588
$is_changed = true; // true, when orientation change
@@ -657,7 +658,7 @@ function image_auto_rotate($image)
657658
*
658659
* @return integer $filesize file size of the image after shrink
659660
*/
660-
function image_auto_length($image, $new_image_size)
661+
public function image_auto_length($image, $new_image_size)
661662
{
662663
// get image dimensions
663664
$img_geo = $image->getImageGeometry();

imcger/imgupload/styles/all/template/event/overall_footer_body_after.html

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,27 @@
305305
* @param int attach_id attach id from attach image
306306
*/
307307
setImgSize: function(attach_id) {
308-
const imgURL = $('#img-' + attach_id).attr('src').replace('&t=1', '');
309-
310-
let ajaxReq = $.ajax({
311-
url: imgURL,
312-
data: null,
313-
type: 'HEAD',
314-
dataType: "json",
315-
timeout: 5000,
316-
});
308+
const url = '{{ U_IUL_IMAGE_SIZE }}',
309+
requestData = 'attach_id=' + attach_id +
310+
'&creation_time=' + $('input[name="creation_time"]').val() +
311+
'&form_token=' + $('input[name="form_token"]').val();
312+
313+
let ajaxReq = $.ajax({
314+
url: url,
315+
data: requestData,
316+
type: 'POST',
317+
dataType: "json",
318+
timeout: 10000,
319+
});
320+
// Code to run if the request succeeds (is done);
321+
ajaxReq.done(function(response) {
322+
if (typeof response !== 'object') {
323+
return;
324+
}
317325

318-
ajaxReq.done(function(response, status, xhr) {
319-
imcger.imgUpload.image.updateImgFileSize(attach_id, xhr.getResponseHeader('Content-Length'));
326+
if (!isNaN(response.fileSize) && response.status < 3) {
327+
imcger.imgUpload.image.updateImgFileSize(response.oldAttachId, response.fileSize);
328+
}
320329
});
321330
},
322331
}

0 commit comments

Comments
 (0)