Skip to main content
2 of 2
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/

Resizing is already done to create the large/medium/thumb sizes, but the problem your facing is that the image is too large to resize, either because of a lack of memory or time.

So resizing isn't an option, if it weren't you wouldn't have your issue. Instead, try limiting the images, so if a 20MB upload happens, it's denied with a message indicating that it needs to be downsized.

Limiting based on image area/megapixels:

<?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');

Limiting based on width or height:

https://wordpress.stackexchange.com/posts/67110/revisions

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

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 ( $width > 2048 )
        $file['error'] = 'Error statement';

    return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'wpse67107_restrict_upload' );

Limiting by area would allow tall/thin or wide/short images that can be resized, limiting by dimensions may be easier to explain

Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147