Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need a way to restrict authors from uploading a images bellow a specific dimensions.

Say I only want to allow uploading images that are at least 400px x 400px. If the image size is smaller, the author should get an error notice that the image is too small.

Is there a plugin or code that can accomplish this?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

Add this code to your theme's functions.php file, and it will limit minimum image dimentions

add_filter('wp_handle_upload_prefilter','tc_handle_upload_prefilter');
function tc_handle_upload_prefilter($file)
{

    $img=getimagesize($file['tmp_name']);
    $minimum = array('width' => '640', 'height' => '480');
    $width= $img[0];
    $height =$img[1];

    if ($width < $minimum['width'] )
        return array("error"=>"Image dimensions are too small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px");

    elseif ($height <  $minimum['height'])
        return array("error"=>"Image dimensions are too small. Minimum height is {$minimum['height']}px. Uploaded image height is $height px");
    else
        return $file; 
}

Then just change the numbers of the minimum dimensions you want (in my example is 640 and 480)

share|improve this answer
    
Thanks! Is there any way to NOT run this function if we are including a post thumbnail? –  Arthur Dos Santos Dias Jan 24 '12 at 11:34
    
This runs every time when you are uploading a file, in the step it is still just a file, before you categorize it or assign it as a thumbnail. You can add a condition based on the file's name with a prefix/suffix of your choice, and name your thumbnailed to be files by this, than do not run the function if file name meets that condition. –  Maor Barazany Feb 21 '12 at 23:38
    
Line 14 references need to have "width" replaced by "height" but otherwise this was exactly what I needed. –  user15580 Apr 26 '12 at 17:36

I prefer not to reformat a colleague's code.
So, this is almost the same answer as @MaorBarazany's, but checking the mime type, changing the file['error'] declaration and changing the function namespace to this wpse Question ID.

Also, the check only occurs for users that are not administrators.

add_action( 'admin_init', 'wpse_28359_block_authors_from_uploading_small_images' );

function wpse_28359_block_authors_from_uploading_small_images()
{
    if( !current_user_can( 'administrator') )
        add_filter( 'wp_handle_upload_prefilter', 'wpse_28359_block_small_images_upload' ); 
}

function wpse_28359_block_small_images_upload( $file )
{
    // Mime type with dimensions, check to exit earlier
    $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );

    if( !in_array( $file['type'], $mimes ) )
        return $file;

    $img = getimagesize( $file['tmp_name'] );
    $minimum = array( 'width' => 640, 'height' => 480 );

    if ( $img[0] < $minimum['width'] )
        $file['error'] = 
            'Image too small. Minimum width is ' 
            . $minimum['width'] 
            . 'px. Uploaded image width is ' 
            . $img[0] . 'px';

    elseif ( $img[1] < $minimum['height'] )
        $file['error'] = 
            'Image too small. Minimum height is ' 
            . $minimum['height'] 
            . 'px. Uploaded image height is ' 
            . $img[1] . 'px';

    return $file;
}

Result of the hook:

blocked image uploads

share|improve this answer
    
Love this and it works like a charm. However, there is one issue: if I want to apply this filter only on certain post types then users can still choose an image from the Media Library which was uploaded in another post type (without the size requirements) which does not meet these requirements. –  cfx May 16 at 1:32

protected by Community May 1 at 20:56

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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