Users running a non-download version of Vanilla (pulled from github), on branch release/2019.016 or master from the last 2 weeks should upgrade to release/2019.017 or latest master for security reasons. Downloaded official open sources releases are not affected.

Is there a way to resize image server side before they download?

This discussion is related to the IndexDiscussionImage addon.

I really like Vanilla forum and this plugin, but it has a severe limitation in that when it displays these tiny image previews - it actually downloads the entire img src="" --- which can be many megabytes. I have many HD photos on my forum (simple phone camera uploads) - so when a user first lands on the forum home page - it takes ages to download a 20mb page with only small images being displayed. The code can be anywhere - in the plugin or elsewhere.

To be honest I don't know about back end coding very much. Would there be a way to resize the image server side before the download? This could be very useful on mobile devices too - where it only downloads an image in the thread with a max-width of 100vw.

I know this sounds simple but is probably difficult to do in practice, otherwise it would have already been done I suppose. But I thought I'd ask.

I found these pages that seem to be doing soemthign similar, but like I said, I don't really know much about the backend, and it would take me days to actually understand what is goinf on in these articles, and how to implement that to Vanilla backend,


Any information or feedback is much appreciated, thank you.

Comments

  • Yes it hurts web performance a lot.

    All profile pictures are entirely downloaded even if vanilla show little profiles pictures (24x24)

    When photos are uploaded, maybe vanilla could save 3 versions (24x24, 100x100 and original picture).

    Gerhard
  • Yes, that's a great idea. Exactly like Wordpress is doing.

    I've spent a few hours looking for a solution to no avail. I don't really understand the theory to troubleshoot the solutions I could find.

    sourabh950
  • charrondevcharrondev Application Developer (PHP, JS) Montreal Vanilla Staff

    Hmm. Vanilla does save profile thumbnails and uses those most of the time. @pioc34 For example your profile thumbnail is sized at 100x100 px. These thumbnails are already generated on image upload and are configurable via the Garden.Thumbnail.Size config.

    This plugin would likely want to do this for the images it uses when posts are created.

    Also 20Mb images are crazy. I'd highly recommend enabling the image size cap on your forum in the dashboard posting settings. That should help.


    pioc34bsourabh299
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    The original question was dealing with that plugin which shows a preview for images in a discussion, nor?

    Looking at the source I see two problems: 1) it does a regex search through the Discussion Body for img-tags. Judging by the quick look I have taken, I'd say it will only work with html, not with BBCode or Markdown and 2) it fetches the original image also from external links which will make it exceptionally slow if the picture is hosted on a slow external server. That will also prevent any image limits: if a user links to a 1GB image, that would be loaded and that cannot be prevented by any setting in Vanilla - that's the nature of an external file...

    The plugin should be rewritten from scratch, I'd say. Main logic should work like that:

    • Look if Discussion->Attributes[ImagePreview] exist
    • If yes, it should be either MediaID = X or MediaID = false (no image available)
    • If no, check GDN_Media for ForeignID = DiscussionID, ForeignTable = discussion. Set Discussion->Attributes accordingly
    • If no and no entry in GDN_Media, parse formatted Discussion Body for img tags, download first image to uploads folder, create thumbnail and save everything done in this step to Media table, update Discussion->Attributes in the last step
    • If MediaID != false, fetch ThumbUrl from Media table and display it



    Gerhard
  • Hi, thank you for taking the time to actually look at the plugin code R_J.

    Unfortunately I don't know nearly enough to actually rewrite the plugin - I imagine it would take double digit hours to do so anyway - and for me to learn from the ground up probably a hundred+... which I don't see myself actually doing at this stage.


    Just as another idea I had in the mean time, that should work from what I can tell, is a CDN caching image-resize option. Cloudflare seems to offer exactly this, but only on their business membership, which is $200/month!

    I'm not a CDN expert and don't know how to really connect vanilla to a CDN. Cloudflare is through CPanel. My Vanilla forum is a sub-folder of a Wordpress site.... I see plugins that seem to do a image-resize caching function on Wordpress.... but I don't understand how I could go about connecting that to Wordpress, but more importantly, my Vanilla forum sub-folder too.

    If anyone has any knowledge to drop or any comments, I'm all ears.

  • Hi @R_J - do you mind if I ask you a question, excuse by lack of knowledge.

    I looked at the plugin's code too, and the search line seems to be:

    preg_match('#\<img.+?src="([^"]*).+?\>#s', $Sender->EventArguments['Discussion']->Body, $images);

    On Line 29. All those symbols after src= are super confusing and weird, but I think that just means find any <img> with src="anything"..... could this line be changed to search for <img src="forum/uploads/...anything" - this would eliminate external images being shown.


    Then my next confusion.... according to my understanding I have found a workaround, but it doesn't work. I can use the "FileUpload" plugin instead of the built-in image upload options. (The built in ones do not create a thumbnail in the post at all - is this normal or maybe my install in broken this way). The image upload for this plugin does create an <img>... but the IndexDiscussionImage plugin doesn't see it and doesn't create the thumbnail in the discussions page. I don't understand why, it is an <img> in the body after all.

    Here are two links, one works and one doesn't, would you have any insight into why?

    https://www.gerhardfourie.com/forum/discussion/97/test3#latest - has the thumbnail, doesn't work.

    https://www.gerhardfourie.com/forum/discussion/96/test2#latest - has the full size image, does work.


    PS if you visit the main discussion page of the forum, you will my problem.... it takes an ugly amount of time to load all those small images as previews.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    There are great online tools which help in learning (or at least understanding) regular expressions. Try this one. Use this as the pattern \<img.+?src="([^"]).+?\>, the "s" is the single line flag that you can choose when you click on the flag symbol.

    I wouldn't use the FileUpload plugin. I think it is no longer supported. And if everything you need is a thumbnail, you really should think about how to get that thumbnail created. Currently I have no idea for a plugin to write and I can imagine trying my hands on an overhauled version.

    Imagine you could design that plugin from ground on up: what would be the features you would like to see:

    1. Should the preview be of the a) first image in the discussion, b) first image in discussion and comments, c) a random picture of the discussion, d) a random picture of all discussions and comments
    2. Should the preview be for external or internal images only?
    3. Should clicking the picture open a) the discussion b) discussion#latest c) the post it links to or display the image in a lightbox without leaving the current page?

    I think I'll try to make those things configurable.

    I don't guarantee I ever finish this, but I will at least start it and upload what I have done on GitHub. If I really do not finish it, you can consider continueing by yourself. I enjoy supporting new developers so I will be happy to help.


    Gerhard
  • Hey @R_J - some big life stuff happened, so I haven't been working on the forum at all for the past few months.

    For now I have just removed the previews - a fast and clean forum is a lot more important than the previews.

    I guess to answer your questions:

    1. It should capture only the first image on the discussion - it should ignore all comments images - even if there are no images in the discussion.
    2. The preview should be for both internal and external
    3. I guess opening a lightbox without leaving the current page would be best - if that is too difficult, second best wold be option a)

    I think myself and many others would get a lot of great use out of such a plugin.

    I'm not expecting anyone to spend hours of their time on this though, unless maybe they see value in it themselves. I know I struggle for time a lot with work and everything. If you do work on this at all, let me know and I'll try to help as best I can.

Sign In or Register to comment.