Halloween Sale 15% off Ends in See the plans
PHP

Minimum and Maximum Image Dimensions for Uploads

Rejects image uploads that are smaller or larger than the pixel sizes you set, so blurry thumbnails and oversized camera originals never reach the Media Library.

Don’t have WPIDE yet? Install it on your site

Code

// Limits in pixels (0 = no limit).
$wpide_min_width  = 300;
$wpide_min_height = 0;
$wpide_max_width  = 8000;
$wpide_max_height = 8000;

add_filter( 'wp_handle_upload_prefilter', function ( $file ) use ( $wpide_min_width, $wpide_min_height, $wpide_max_width, $wpide_max_height ) {
    if ( ! empty( $file['error'] ) || empty( $file['type'] ) || 0 !== strpos( $file['type'], 'image/' ) || 'image/svg+xml' === $file['type'] ) {
        return $file;
    }

    $size = wp_getimagesize( $file['tmp_name'] );

    if ( ! $size ) {
        return $file;
    }

    list( $width, $height ) = $size;

    if ( ( $wpide_min_width && $width < $wpide_min_width ) || ( $wpide_min_height && $height < $wpide_min_height ) ) {
        $file['error'] = sprintf( 'This image is %1$d x %2$d pixels. Images must be at least %3$d x %4$d pixels.', $width, $height, $wpide_min_width, $wpide_min_height );
    } elseif ( ( $wpide_max_width && $width > $wpide_max_width ) || ( $wpide_max_height && $height > $wpide_max_height ) ) {
        $file['error'] = sprintf( 'This image is %1$d x %2$d pixels. Images can be at most %3$d x %4$d pixels.', $width, $height, $wpide_max_width, $wpide_max_height );
    }

    return $file;
} );

Notes

Set any limit to 0 to switch it off. SVG files are not checked.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.