Limit image resolution uploading to wordpress

Limit image resolution uploading to wordpress

Posted: 7 years ago in  Wordpress | PHP |


For start-up website, the host with low resource would help you save money. Hence, there should be a limit in upload files. For website using wordpress, its very easy to hook in and check the image size then block it if size is too big.


For start-up website, the host with low resource would help you save money. Hence, there should be a limit in upload files. For website using wordpress, its very easy to hook in and check the image size then block it if size is too big.

Lets create functions.php in your theme folder or create a wordpress plugin, in this example, I will limit image size less than 1024x768

add_filter('wp_handle_upload_prefilter','custom_validate_image_size');
function custom_validate_image_size( $file ) {
    $image = getimagesize($file['tmp_name']);
    $maximum = array('width' => '1024', 'height' => '768');
    $width= $image[0];
    $height =$image[1];

    if ($width > $maximum['width'] || $height > $maximum['height'])
        return array("error"=>"Image dimensions are too big. Maximum width is {$maximum['width']}px. Maximum height is {$maximum['height']}px.");
    else
        return $file; 
}

Then you are good to go for couple of month with low-resource host before you are able to upgrade