If image governance is not performed, users can upload massive image sizes, which has many adverse effects (e.g., Poor User Experience, Server Performance Degradation, Massive Server Disk Space Usage).
It’s better to start as restrictive as possible and then increase the limit based on user feedback. Users will better accept you easing up on restrictions rather than making things more strict.
As this code snippet is currently configured, it sets a max file upload size of 128 KB, which is still a considerable image file size. I don’t recommend allowing larger image file sizes.
Check out the Image Optimization for MyListing Websites guide for more information.
Customization Notes
Adjust the highlighted rows below (3, 6, 7) with the desired values. It’s critical that you are consistent with whatever limit you want to set, so let’s look at some examples.
256 KB Limit
- Row 4: 256
- Row 7: 256
- Row 8: 256 KB
512 KB Limit (i.e., .5 MB)
- Row 4: 512
- Row 7: 512
- Row 8: 512 KB
1MB Limit
- Row 4: 1024
- Row 7: 1024
- Row 8: 1 MB
function max_image_size($file)
{
$size = $file["size"];
$size = $size / 128;
$type = $file["type"];
$is_image = strpos($type, "image") !== false;
$limit = 128;
$limit_output = "128 KB"; // Upload error message: 'Image files must be smaller than 128 KB'
if ($is_image && $size > $limit) {
$file["error"] = "Image files must be smaller than " . $limit_output;
} //end if
return $file;
} //end max_image_size()
add_filter("wp_handle_upload_prefilter", "max_image_size");