Image upload error when using resizer

AI-generated summary

Users report an image upload error when uploading large PNGs that need resizing to meet the max image size kb limit, resulting in a cryptic convert: improper image header error. The issue appears to stem from Discourse attempting to convert PNGs to JPEGs during resizing, which causes ImageMagick to fail due to incompatible operations. Users note that renaming the file to .jpeg bypasses the issue, and the error message is unhelpful. A discussion ensues about whether the client_max_body_size setting in nginx or apache might be involved, though it’s clarified that this is unrelated. The problem is resolved by adjusting the max image size kb setting, but users suggest a more user-friendly error message should be shown. The issue is traced to a bug in the image optimization pipeline where PNG-specific operations are applied to JPEGs. dan confirms the fix, which prevents the pipeline from misapplying operations and improves error handling.

If you try to upload a large PNG which needs to be resized to comply with max image size kb you get an error:

lib/discourse.rb:57:in `exec': convert: improper image header `/tmp/image20200221-243-17ldizm.jpg' @ error/png.c/ReadPNGImage/4294.

If you change the extension of the image to .jpeg it uploads fine!

It also works fine if you reset max image size kb to the default, however, it seems to be renaming the image as a jpeg regardless.

Here’s the image: Dropbox


Edit: I’ve also just noticed this under the max image size kb option:

The maximum image upload size in kB. This must be configured in nginx (client_max_body_size) / apache or proxy as well.

I don’t think I’ve done that - could it be causing it? (any examples on where to edit this?)

1 Like

It should be possible to increase that value by adding an upload_size parameter to your site’s app.yml file. See Change the maximum attachment/upload size for details.

5 Likes

I changed max image size kb to 500k and it uploaded, I wonder though if the error message shown to users should simply be something like “Upload failed: image size too large, please resize and try again”?

5 Likes

The error message is really bad, though I wonder how many people change that setting in the first place?

5 Likes

I can’t remember for sure now, but IIRC if you lower that setting and an uploaded image is larger than that, Discourse will attempt to resize the image to bring it down.

I think it’s tied to the png to jpg quality setting? It was a while ago now but I think Sam worked on the image resizer.

With regards to anyone who’d want to change it (or rather want smaller image sizes) I’d imagine any site where image quality is less important (so most sites apart from photography sites).


Quick search returned this - so looks like the resizer hasn’t been implemented yet (but I was sure it had :thinking:)

1 Like

That was my understanding too… that there is downsizing in place that processes large upload on the server.

EDIT/UPDATE:

It has been pretty well described here now:

5 Likes

@dan can you find a way to have a better error message when a user is trying to upload a very large image? Low priority.

4 Likes

I fixed the two problems here:

  1. The image optimization pipeline converted PNGs to JPEGs and then tried to optimize resulted JPEGs using the operations for PNG images.

  2. ImageMagick errors were returned to the user, but were not helpful at all (e.g. convert: improper image header...)

https://github.com/discourse/discourse/pull/9257

8 Likes