Introduction

When uploading large images to WordPress, the media upload process may fail with a generic "HTTP error" message. The underlying cause is often ImageMagick running out of memory while generating thumbnails. The PHP error log reveals:

bash
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 24576 bytes) in /var/www/html/wp-includes/class-wp-image-editor-imagick.php

WordPress defaults to using ImageMagick (via the Imagick PHP extension) for image processing, which can require significant memory for large images.

Symptoms

  • Media upload fails with generic "HTTP error" after progress bar completes
  • Large images (over 5MP) fail while smaller images upload successfully
  • PHP error log shows memory exhaustion in class-wp-image-editor-imagick.php
  • Upload succeeds but no thumbnails are generated
  • Server has sufficient RAM but PHP memory_limit is too low

Common Causes

  • PHP memory_limit (default 128MB or 256MB) insufficient for ImageMagick processing
  • ImageMagick's own resource limits in policy.xml restrict memory usage
  • Upload images with very high resolution (e.g., 20MP+ camera photos)
  • Multiple thumbnail sizes configured, each requiring a separate ImageMagick operation
  • Shared hosting environment with strict memory limits

Step-by-Step Fix

  1. 1.Increase PHP memory limit in php.ini or .htaccess:
  2. 2.```ini
  3. 3.memory_limit = 512M
  4. 4.`
  5. 5.For WordPress specifically, add to wp-config.php:
  6. 6.```php
  7. 7.define('WP_MEMORY_LIMIT', '512M');
  8. 8.define('WP_MAX_MEMORY_LIMIT', '512M');
  9. 9.`
  10. 10.Configure ImageMagick resource limits in /etc/ImageMagick-6/policy.xml:
  11. 11.```xml
  12. 12.<policymap>
  13. 13.<policy domain="resource" name="memory" value="2GiB"/>
  14. 14.<policy domain="resource" name="map" value="4GiB"/>
  15. 15.<policy domain="resource" name="width" value="16KP"/>
  16. 16.<policy domain="resource" name="height" value="16KP"/>
  17. 17.<policy domain="resource" name="area" value="1GP"/>
  18. 18.<policy domain="resource" name="disk" value="8GiB"/>
  19. 19.</policymap>
  20. 20.`
  21. 21.Switch to GD library as the image editor (uses less memory than ImageMagick):
  22. 22.Add to your theme's functions.php:
  23. 23.```php
  24. 24.add_filter('wp_image_editors', function($editors) {
  25. 25.return ['WP_Image_Editor_GD', 'WP_Image_Editor_Imagick'];
  26. 26.});
  27. 27.`
  28. 28.This places GD first in the editor preference order.
  29. 29.Limit maximum image dimensions to prevent processing excessively large images:
  30. 30.```php
  31. 31.// Add to functions.php
  32. 32.add_filter('big_image_size_threshold', function($threshold) {
  33. 33.return 2560; // Resize images larger than 2560px
  34. 34.});
  35. 35.`
  36. 36.WordPress will automatically resize oversized images before processing thumbnails.
  37. 37.Reduce the number of generated thumbnail sizes by removing unused sizes:
  38. 38.```php
  39. 39.// Remove default sizes you don't need
  40. 40.add_action('init', function() {
  41. 41.remove_image_size('medium_large');
  42. 42.remove_image_size('1536x1536');
  43. 43.remove_image_size('2048x2048');
  44. 44.});
  45. 45.`

Prevention

  • Set big_image_size_threshold to automatically resize uploads exceeding 2560px
  • Monitor PHP memory usage during uploads with New Relic or similar APM tools
  • Use an image optimization plugin (ShortPixel, Imagify) that processes images via API instead of server-side
  • Configure server-level image size limits in your upload form and Nginx client_max_body_size
  • Keep ImageMagick policy.xml resource limits in version control as part of server configuration
  • Test media uploads with large images (10MB+) during server setup to verify configuration