I had a WordPress client site today where, recently, image uploads have been failing. Initially this seemed sporadic and I couldn’t replicate it, but eventually I could and an image upload failed every time. Here’s what we were seeing – see the error on the right (apologies for obscured client-data and poor quality image):

File upload HTTP error

There was nothing particularly apparent in the browser’s developer tools – there was a request to wp-admin/async-upload.php which just seemed to time out or not respond at all. Analysis of server-side errors showed nothing either. So I was baffled.

So I did some searching and found this support thread. Here’s the explanation and fix.

Explanation

When you upload an image to WordPress, WordPress automatically creates several versions of it at different sizes.  The support thread suggests that WordPress can use one of a number of image processing libraries to do this, and it has a preferred order for trying these libraries.  It would seem that, by default, WordPress prefers a library called ImageMagick over a library called GD.

Now, for whatever reason, either ImageMagick had decided to stop working properly (possibly it was upgraded on the hosting server?) or WordPress’s preferred order changed during an update.  I don’t know the reason why the behaviour suddenly changed in the case of this site.

In any case, the fix is to tell WordPress to use a different preferred order of image processing libraries.

Fix

And it’s exactly right. I added the code from the GitHub version of the plugin that’s mentioned to my functions.php file. This forced WordPress to try using the GD library before it uses ImageMagick and all was good again.

Here’s the code I added:

function ms_image_editor_default_to_gd( $editors ) {
	$gd_editor = 'WP_Image_Editor_GD';
	$editors = array_diff( $editors, array( $gd_editor ) );
	array_unshift( $editors, $gd_editor );
	return $editors;
}
add_filter( 'wp_image_editors', 'ms_image_editor_default_to_gd' );