The image_memory_limit WordPress PHP Filter allows you to modify the memory limit allocated for image manipulation.
Usage
add_filter('image_memory_limit', 'my_custom_image_memory_limit'); function my_custom_image_memory_limit($filtered_limit) { // your custom code here return $filtered_limit; }
Parameters
$filtered_limit
(int|string) – Maximum memory limit to allocate for images. Default is WP_MAX_MEMORY_LIMIT or the original php.ini memory_limit, whichever is higher. Accepts an integer (bytes), or a shorthand string notation, such as ‘256M’.
More information
See WordPress Developer Resources: image_memory_limit
Examples
Increase image memory limit to 512M
Increase the memory limit allocated for image manipulation to 512M.
add_filter('image_memory_limit', 'increase_image_memory_limit'); function increase_image_memory_limit($filtered_limit) { return '512M'; }
Double the current memory limit
Double the current memory limit for image manipulation.
add_filter('image_memory_limit', 'double_image_memory_limit'); function double_image_memory_limit($filtered_limit) { $current_limit = wp_convert_hr_to_bytes($filtered_limit); return 2 * $current_limit . 'B'; }
Set image memory limit based on user role
Set the image memory limit based on the user role. Administrators get a higher limit.
add_filter('image_memory_limit', 'set_image_memory_limit_by_role'); function set_image_memory_limit_by_role($filtered_limit) { if (current_user_can('administrator')) { return '512M'; } return '256M'; }
Limit image memory to half the server’s memory limit
Limit the image memory to half of the server’s memory limit.
add_filter('image_memory_limit', 'limit_image_memory_to_half_server_limit'); function limit_image_memory_to_half_server_limit($filtered_limit) { $server_memory_limit = wp_convert_hr_to_bytes(ini_get('memory_limit')); return intval($server_memory_limit / 2) . 'B'; }
Log image memory limit changes
Log any changes to the image memory limit in a custom log file.
add_filter('image_memory_limit', 'log_image_memory_limit_changes', 10, 2); function log_image_memory_limit_changes($filtered_limit, $original_limit) { if ($filtered_limit !== $original_limit) { error_log("Image memory limit changed from $original_limit to $filtered_limit"); } return $filtered_limit; }