Fixing large image upload timeouts on WordPress

Just to clarify, this is specifically for WordPress (version 5.8 in my case) running on Ubuntu and Nginx.

To follow up on my post from the other day, while I was able to get the image to at least finally able to get an image uploaded, it seemed it was still mostly a miss when it came to post-processing the image, specifically larger ones (20-30+ MB).

In my case, while using the uploader, while no longer receiving the error regarding “Suggested maximum size is 2500 pixels”, I noticed that there was absolutely NO post-processing happening. A single image would have no resized variants in the wp-content/uploads/2021/08/ folder (in my case). Meaning, there are no thumbnails and no small/medium/large variants, so even a preview of the image was loading/rendering the full 12000x9000px image.

Yuck. After some digging, it turned out the problem lied with both the new ImageMagick extension being used, as well as extending the timeout for uploading/processing images.

The first change is to adjust the policy.xml file for ImageMagick. I found mine in the default location of /etc/ImageMagick-6/policy.xml. I first adjusted the disk setting from 1GiB to 5GiB. You may or may not need to adjust map, as I increased this really just in case.

<policymap>
  <policy domain="resource" name="map" value="1GiB"/>
  <policy domain="resource" name="disk" value="5GiB"/>
</policymap>

After adjusting these, I noticed that resized images were starting to appear in my wp-content folder, such as “Xxx-scaled.jpg”, along with additional files for each dimension. However, since the server was taking additional time to process these larger images (as now allowed by the setting change above), the upload request in the browser was timing out. Specifically, it was hitting the 60 second request/read timeout configured in Nginx.

To increase the timeout, I added the following setting to /etc/nginx/nginx.conf:

http {
	...
	fastcgi_read_timeout 300;
	...
}

It’s essentially extending the read timeout to 5 minutes for PHP processing. This will allow more time for the server to process the image resizing.

With these two items adjusted, I’m able to upload large images and allow it to generate all resized versions.

But wait, why is it taking so long?

Why is it taking to long to make a couple resizes? Surely my decently-powered server isn’t being crippled by a 20MB image, right?

This leads me to yet another weird quirk I’ve noticed after the resizing was fixed – it’s resizing 10 times! Here is the directory list for one of the upload folders:

total 24592
-rw-rw-r-- 1 www-data www-data 20899309 Aug 10 16:50 20210807_114924.jpg
-rw-rw-r-- 1 www-data www-data  1558883 Aug 10 16:51 20210807_114924-scaled.jpg
-rw-rw-r-- 1 www-data www-data    82553 Aug 10 16:51 20210807_114924-225x300.jpg
-rw-rw-r-- 1 www-data www-data    66595 Aug 10 16:51 20210807_114924-150x150.jpg
-rw-rw-r-- 1 www-data www-data   345543 Aug 10 16:51 20210807_114924-768x1024.jpg
-rw-rw-r-- 1 www-data www-data   679444 Aug 10 16:52 20210807_114924-1152x1536.jpg
-rw-rw-r-- 1 www-data www-data  1090133 Aug 10 16:52 20210807_114924-1536x2048.jpg
-rw-rw-r-- 1 www-data www-data   100992 Aug 10 16:52 20210807_114924-300x400.jpg
-rw-rw-r-- 1 www-data www-data    90766 Aug 10 16:52 20210807_114924-350x260.jpg
-rw-rw-r-- 1 www-data www-data   182116 Aug 10 16:52 20210807_114924-680x500.jpg
-rw-rw-r-- 1 www-data www-data    60817 Aug 10 16:52 20210807_114924-86x60.jpg

This is why it’s now taking so long to process! This is why I had to increase the timeouts! I’m sure if it was only resizing a couple times, it would happen within the default 60 second timeout.

Until next time I suppose.

Also, I’ll count this toward day 4 of #100DaysToOffload.

Leave a Reply

Your email address will not be published. Required fields are marked *