jdaehne / ImageOptimOnUpload

ImageOptimOnUpload
6 stars 1 forks source link

working with drag'n 'drop, but not on File Manager Upload #4

Closed 70hundert closed 4 years ago

70hundert commented 4 years ago

Hi there!

When I want to add an image (original dimensions = 4001x2250px) in a resource via drag'n'drop into the content-input-field, the extra/plugin is working as expected (sytem-setting: imageoptimonupload.options - "1920"; the image gets uploaded with the correct width of 1920px)

But when I'm using the File Manager Upload, the plugin does not work. The image keeps the original dimensions 4001x2250px.

I'm getting this error message:


[2020-03-02 11:03:25] (ERROR @ /var/www/domain.org/htdocs/domain.de/modx/core/cache/includes/elements/modplugin/8.include.cache.php : 41) PHP warning: opendir(/var/www/domain.org/htdocs/domain.de/modx/veranstaltungen/2020-02/): failed to open dir: No such file or directory
[2020-03-02 11:03:28] (ERROR @ /var/www/domain.org/htdocs/domain.de/modx/core/cache/includes/elements/modplugin/26.include.cache.php : 100) imageOptimOnUpload (API Error): 
[2020-03-02 11:03:28] (ERROR @ /var/www/domain.org/htdocs/domain.de/modx/core/model/modx/modx.class.php : 1671) [OnFileManagerUpload]array(10) {
  [0]=>
  string(37) "HTTP/1.1 404 Could not download image"
  [1]=>
  string(35) "Date: Mon, 02 Mar 2020 10:03:28 GMT"
  [2]=>
  string(49) "Content-Type: application/im2+json; charset=utf-8"
  [3]=>
  string(19) "Content-Length: 268"
  [4]=>
  string(17) "Connection: close"
  [5]=>
  string(30) "Server: ImageOptim/5.27.1 (qp)"
  [6]=>
  string(12) "Vary: Accept"
  [7]=>
  string(23) "cache-control: no-cache"
  [8]=>
  string(25) "im2-status: 404 UserError"
  [9]=>
  string(229) "im2-error: "Could not download image\nRequest to https://domain.de/modx/assets/images/veranstaltungen/2020-02/ShrinkingSpaces_Facebook_200204.png failed. The server at domain.de returned HTTP error 404 Not Found.""
}
1

I expected the plugin to work by using the file manager. What am I doing wrong?

MODX Version 2.7.2, imageoptimonupload-1.0.2-beta1

70hundert commented 4 years ago

Hello again!

I guess this is related to the Redactor-extra from modmore.

The way I described to add an image via drag'n'drop worked with Redactor 2, but both ways - the drag'n'drop-way and the FileManager-Upload - do not fire the plugin at all in Redactor 3.

I tested the use of your this extra on a modx-installation without Redactor, there the image upload via the FileManager does fire the plugin.

Even with the MoreGallery-extra from modmore I tried this. I edited the plugins systems-settings and added "MoreGallery_OnImageCreate", but this also does not work.

Do you have any idea how to solve these issues?

jdaehne commented 4 years ago

Hi @70hundert ,

the Plugin should work on every Upload that triggers the event "OnFileManagerUpload". That is the case using the File-Manager. I just tested it and it works as expected. The error Message tells us, that the Plugin is involved but there seems to be something wrong downloading the optimized image from the Server. So maybe that is just a temporarily problem by imageOptim?

The other Problem is, that moreGallery and may Redactor 3 may not trigger the action "OnFileManagerUpload" - that way the plugin will never be involved. I will try to check this.

Mark-H commented 4 years ago

Both extras use media sources to upload images; that should be triggering all the events.

Mark-H commented 4 years ago

It looks like the plugin is looking specifically for an upload name of file in the files array:

$file = $modx->event->params['files']['file'];

In Redactor the name of upload is upload, so $file is going to be empty there.

I'm 99% sure it'll work consistently no matter the name of the upload if you'd change to this:

$modx->event->params['file']

In the media source (modFileMediaSource, line 851), it's already looping over all the files to trigger the event once per uploaded file.

Or even if you remove that line, because the way MODX runs events it will fill the params as "top level" properties. So the event will set a $files, $file and $directory parameter already.

Tho I can't really make sense of that API error, I wouldn't expect it to even know the filename?

jdaehne commented 4 years ago

Hi @Mark-H,

thanks a lot for your support. I will check this and publish an Update of the extra.

70hundert commented 4 years ago

Thank you guys for diving into this issue and trying to sort it out!

jdaehne commented 4 years ago

@Mark-H i just dived into this a little and checked that the var $files will return an array. $file does not exist. That way i receive different arrays:

In moreGallery i get:

(
    [upload] => Array
        (
            [name] => 6_AdobeStock_63473115.jpeg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpyblire
            [error] => 0
            [size] => 4817368
        )
)

And using the Filemanager i get the array:

(
    [file] => Array
        (
            [name] => 6_AdobeStock_63473115.jpeg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpyblire
            [error] => 0
            [size] => 4817368
        )
)

I can check if "file" does not exist and look for "upload". But that feels a bit dirty. Do you have a nice solution? Thanks a lot.

$file = (empty($files['file'])) ? $files['upload'] : $files['file'];
Mark-H commented 4 years ago

There should definitely be a $file which just contains the inner array looking at the core code..

If that doesn't work, another way to do it without needing to know if it's called "file" or "upload" or whatever is to use reset() to get the first element in the outer array:

$file = reset($files);

Or, as in theory there could be multiple files, iterate over the array:

foreach ($files as $file) { 
   // do your magic
}

(I don't think anything available today actually does multiple files once)

jdaehne commented 4 years ago

@70hundert i just updated the extra to version 1.0.3 with the help of @Mark-H . The extra should now support all the modmore extras.

I would be happy if you could also test it.

Mark-H commented 4 years ago

Will give it a try tomorrow, thanks for your great work ;)

70hundert commented 4 years ago

Hi Jan!

Thanks again for your work on this - and of course to you too, Mark-H!

I just upgraded the extra and tested it - but with no effect.

I did try the Drag'n'Drop-way inside the content-input plus in the redactor image-modal, clicking in the redactor-image-modal for uploading and choosing my file through the windows-explorer, plus an upload via the Modx-Manager, but none of the ways i tried did resize the image, it has still it's original dimensions = 4001x2250px (sytem-setting: imageoptimonupload.options - "1920") and size 6.616 kb.

I even cleared the cache-folder manually and tried a different browser.

jdaehne commented 4 years ago

@70hundert that is very strange - because i tested it with CB, Redactor and moreGallery and everything worked as expected. Strange - maybe we can have a look on it together via TeamViewer or would you mind to grant us access?

Please just send us an email to support@quadro-system.de and i will try to fix this.

jdaehne commented 4 years ago

@70hundert just fixed the issue in the current Version 1.0.4