# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Media](https://wpide.com/library/category/media/)
- Tags: uploads, images, dimensions
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/image-upload-dimension-limits/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Minimum and Maximum Image Dimensions for Uploads" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
