Image upload size/resize question

After reading through this topic, and this one, and this one, I’m not entirely sure what Discourse does with large images. My previous understanding is that there is no software resizing done, just CSS - that image size was limited by maximum image upload size in kB and that was that. If a larger image is a remote file it gets linked, otherwise you just can’t upload it.

But now I’m confused because those topics mention downsizing and optimizing, which I didn’t think was happening because I’ve never seen it in practice that I’ve been aware of. I also have no idea what the Maximum number of megapixels allowed for an image limit applies to. Does this prevent uploading images larger than that, or do images that exceed that limit get resized so long as they’re < max upload size?

If it’s the latter, why is the default setting for megapixels so huge relative to the default max upload size? It seems extraordinarily unlikely that somebody will want to post a 51 megapixel image that is smaller than 4096 kB.


What I want is for users to conveniently upload whatever they have without worrying about file sizes and resolutions, and for the software to deliver something appropriate that doesn’t kill mobile users and people on slower connections. Is this currently possible just by adjusting max image size and max megapixels?

4 Likes

This all works out of the box:

Right click on :arrow_double_up: view source, the image is small. Click on it and you see the full res image.

4 Likes

So if I change max upload to 10 MB and max megapixel to 12, what happens when a user tries to upload a 16 megapixel image that’s 7 MB?

Under the default settings a significant percentage of pictures taken by newer phones are larger than 4096 kB, but nowhere near 40 megapixels.

I’m still not understanding what happens when, or why the defaults are set to what they are.

1 Like

I recommend playing with the settings, change a setting, upload a huge image, see what happens.

3 Likes

OK, I can do that. However, I would also like to understand what the software is actually doing so I can make more informed decisions. In the time I’ve been using Discourse I’ve learned to respect the default settings and only change them when necessary. In this instance, the defaults aren’t working very well for a fairly typical community and I am curious why they are set as they are, what reasonable values I should consider instead, and what will happen when I make those changes.

1 Like

Reading the code, max megapixels is a hard restriction. It is a safety switch not something the system uses to make decisions about downsizing with.

max_image_size_kb is the sole deciding factor for auto downsizing on upload.

Bump up max megapixels to the absolute largest amount of megapixels you would ever want to host.

3 Likes

After mashing buttons for awhile to figure things out, this is what I think is happening:

  • With a default install, client_max_body_size == max image size kb, images will not be resized.

  • Increasing client_max_body_size will allow larger uploads, which Discourse will then attempt to resize under the limit set in max image size kb

  • Despite the description which implies otherwise, max image size kb doesn’t actually limit upload sizes (this is super confusing!)

  • Upload sizes are solely set with nginx’s client_max_body_size

  • When resizing, JPEG’s stay JPEG’s with good results
    PNG’s are converted to JPG’s with acceptable results
    Animated GIF’s stay GIF’s with generally lousy results

Is this all correct? If so, it seems like the sort of thing that should be explained a little better since an out of the box installation doesn’t do any image resizing even though that is a feature many people might want and it isn’t clear at all from available documentation how to enable it.

5 Likes

Hmm, I just looked at nginx.sample.conf on github and it seems that the default for client_max_body_size is now 10 MB rather than 4 MB. Has this always been the case? I don’t recall ever making a change to that file on my install before today.

3 Likes

Thanks for thorough testing with current version. I’ve checked it and it looks like that works in practice just as you described.

1 Like

Max body size has been 10 for quite a while, we should probably increase it to 20

Agree we should improve the site setting description, will do so later today.

EDIT:

Documentation is updated per:

12 Likes

Hi Sam, going by the description there doesn’t seem to be working for me:

+ max_image_size_kb: “The maximum image upload size in kB. This must be configured in nginx (client_max_body_size) / apache or proxy as well. Images larger than this and smaller than client_max_body_size will be resized to fit on upload.”

+ max_image_megapixels: “Maximum number of megapixels allowed for an image. Images with a higher number of megapixels will be rejected.”

Or at least no resizing seems to be attempted.

I had max_image_size_kb set to 1000kb and an image slightly over that was replaced by a link and followed by text which says (image larger than 1000KB).

I changed the setting to 500kb and the same thing happened.

I haven’t changed client_max_body_size in NGINX as I assume it is at the default (of 4096).

These are on images that are hot linked btw (where I assume if you have download remote images to local checked) it should treat as a standard upload? (If not can we get it to behave the same way please?)

Edit: Ok so uploading directly and the resizing works - yay! So it’s just the remote images that are being fetched that fail. Not sure what behaviour would be ideal tbh, but on first thought I would think the same - those images get resized too.

4 Likes

I think this is a separate feature request. Certainly if you push max image size down to 200k, it totally makes sense to just resize the hotlinked images when we download them. There are thresholds at play, we should not download a 700 gigabyte image just to figure this out, but yeah something could be a bit better.

5 Likes

I agree Sam. I can’t think of any reason why you wouldn’t want downloaded hot linked images to follow the same rules tbh.


I also just discovered that if you change the max image size, that actually has an impact on what file size you end up with - which is really cool! Setting it to 500kb ended up resizing a 1.2mb image to 360kb, setting it to 200kb, resulted with the same file at 118kb :+1: (though obviously it is smaller as well).

Edit: Argh, but we get that original problem again - where images that are too large to be resized fail with:

lib/discourse.rb:57:in `exec': convert: improper image header `/tmp/image20200228-23391-t365z9.jpg' @ error/png.c/ReadPNGImage/4294.

I wonder if there is a way where DC will always do a resize? Even if the image ends up being tiny?

4 Likes


This is the default setting on our forums. I tried to change it to 20000 and it worked (or appeared to stick), but when I went to upload an image/GIF that was 17.2mb I was greeted with this error message:

image
Can you tell me what path I would go to (commands to enter) in linux, where I can change the “client_max_body_size” in nginx? I’m on a digitalocean droplet.

Does this mean the default will soon be changed from 4096 to 20000?

4 Likes
4 Likes

Brilliant @riking! Thanks, mate!

1 Like