I need a way restrict authors from uploading a images bellow specific dimensions. Say I only want to allow uploading images that are at least 400px x 400px. If the image is smaller, the author should get a error notice that the image is too small. Is there a plugin or code that can accomplish this? Thanks in advance.

link|improve this question
feedback

1 Answer

up vote 7 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)

link|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 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 at 23:38
Line 14 references need to have "width" replaced by "height" but otherwise this was exactly what I needed. – Hi1 Apr 26 at 17:36
feedback

Your Answer

 
or
required, but never shown

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