3

Clients have been uploading huuuge images, then complaining that the server is running out of memory.

Ofcourse one could bump the amount of memory, but this just introduces an arms race.

What hooks can I add to impose a maximum image size ( dimensions, not filesize ) on files uploaded, e.g. No you can't upload that 8 Megapixel shot, resize it first to < 2 Megapixels.

To be clear, I am talking about image size, not file size, aka Image height, and Image width

3 Answers 3

6

Basically you just retrieve the info via getimagesize(), a basic PHP function, a then handle your errors with notes.

The plugin

A basic plugin as a starting point:

<?php
/** Plugin Name: (#67107) »kaiser« Restrict file upload via image dimensions */

function wpse67107_restrict_upload( $file )
{
    $file_data = getimagesize( $file );
    // Handle cases where we can't get any info:
    if ( ! $file_data )
        return $file;

    list( $width, $height, $type, $hwstring, $mime, $rgb_r_cmyk, $bit ) = $file_data;

    // Add conditions when to abort
    if ( 3200728 < $width * $height )
    {
        // I added 100k as sometimes, there are more rows/columns 
        // than visible pixels, depending on the format
        $file['error'] = 'This image is too large, resize it prior to uploading, ideally below 3.2MP or 2048x1536 px.';
    }

    return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'wpse67107_restrict_upload' );
5
  • I'm aware of getimagesize =p
    – Tom J Nowell
    Oct 4, 2012 at 11:50
  • How can we get that 3200728 from WordPress dynamically? How to find what the max width/height is for WordPress?
    – kodfire
    May 12, 2022 at 7:24
  • @kodfire I would try to fetch your registered image sizes and extract it from there, then calculate the size from each width/height setting and stick with the largest number.
    – kaiser
    May 12, 2022 at 10:12
  • @kaiser But those sizes for generating images are different from the size limitation that WordPress applies to them. So for example if you have sizes: 100x100, 200x200, and 300x300, and if the limitation of WordPress is 250, the 300x300 will be resized to the maximum which is 250 here.
    – kodfire
    May 14, 2022 at 4:59
  • @kodfire The question and answer is 10 years old and I do not work with WP these days. IIRC, there's no real width/ height limit, but just the PHP servers max upload file limit in place as defined in the php.ini (or the PHP FPM pools ini). The 3200728 probably just were a random number, set to show any working abort condition pixel amount for the example code. Also note Toms comment on Ravi Kumars answer below about the upload max file size.
    – kaiser
    May 14, 2022 at 21:10
2

Here's my take on it

<?php
/**
 * Plugin Name: Deny Giant Image Uploads
 * Description: Prevents Uploads of images greater than 3.2MP
 */

function tomjn_deny_giant_images($file){
    $type = explode('/',$file['type']);

    if($type[0] == 'image'){
        list( $width, $height, $imagetype, $hwstring, $mime, $rgb_r_cmyk, $bit ) = getimagesize( $file['tmp_name'] );
        if($width * $height > 3200728){ // I added 100,000 as sometimes there are more rows/columns than visible pixels depending on the format
            $file['error'] = 'This image is too large, resize it prior to uploading, ideally below 3.2MP or 2048x1536';
        }
    }
    return $file;
}
add_filter('wp_handle_upload_prefilter','tomjn_deny_giant_images');
4
  • Aside from different expression of your function, whats the difference between this and Kaiser's answer?
    – Adam
    Oct 4, 2012 at 11:51
  • @kaiser answer limits based on maximum heights and widths, whereas mine limits based on maximum area. So my code would allow a 10x3000 longcat image (0.03MP), Kaisers wouldn't.
    – Tom J Nowell
    Oct 4, 2012 at 11:56
  • Also my code has a more descriptive error message
    – Tom J Nowell
    Oct 4, 2012 at 12:00
  • There was that, and mine checks if the file is an image before grabbing the size, his doesn't
    – Tom J Nowell
    Oct 4, 2012 at 12:08
0

I understand that you only need to deny uploads based on Image Dimension. But I just want to add a note, this will not prevent your server from getting Out of Memory. Image dimensions is available once the data has been fully uploaded on the server (like Apache), and hence server memory is consumed.

You should consider limiting the Upload size at Apache and PHP level, and then if you want, you can also restrict at Wordpress level for Image Dimension. To set the max upload size in Apache,

<Directory "/var/www/wordpress-site/wp-uploads">
    LimitRequestBody 10485760
</Directory>

In php.ini, set:

memory_limit = 64M
upload_max_filesize = 10M
post_max_size = 20M
1
  • 2
    Ah but the problem is that while I can upload say a 10MB jpeg, it is not 10MB in memory when it's decompressed into a RAW image in RAM for processing. For example a 30kx30k pixel white image with no detail is huge in memory, but as a PNG file it's <1kb thanks to compression (30000x30000x4(rgba)x4(bytes for each channel) = 14400000000 bytes or 14062500kb or 13732MB or 13GB
    – Tom J Nowell
    Oct 4, 2012 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.