1

I want to restrict the dimensions of images uploaded by anyone who is not an administrator in WordPress to a specific size. I also want to return a specific error feedback when the image uploaded doesn't meet the requirements.

For example: If I set the restriction to width of 500px and height of 700px, if a user tries to upload anything different from my specified width and height, it will be rejected and return a message that says, image size should be of size 500px by 700px in jpg format only.

I found a solution that only requires minimum dimensions but it's a little bit away from what I want (How to Require a Minimum Image Dimension for Uploading?).

I have added @clayRay's suggestion, help check this:

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' => 500, 'height' => 500 );
    $maximum = array( 'width'  $maximum['width'] )
        $file['error'] = 
            'Image too large. Maximum width is ' 
            . $maximum['width'] 
            . 'px. Uploaded image width is ' 
            . $img[0] . 'px';

    elseif ( $img[1] > $maximum['height'] )
        $file['error'] = 
            'Image too large. Maximum height is ' 
            . $maximum['height'] 
            . 'px. Uploaded image height is ' 
            . $img[1] . 'px';

    return $file;
}

1 Answer 1

0

I've modified this answer from brasofilo. Modify the width and height values in the following line in the below code to your needs...

    $maximum = array( 'width' => 500, 'height' => 700 );

... and then add the code to your theme's functions.php file.

add_action( 'admin_init', 'wpse_371740_block_authors_from_uploading_large_images' );

function wpse_371740_block_authors_from_uploading_large_images()
{
    if( !current_user_can( 'administrator') )
        add_filter( 'wp_handle_upload_prefilter', 'wpse_371740_block_large_images_upload' ); 
}

function wpse_371740_block_large_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'] );
    $maximum = array( 'width' => 500, 'height' => 700 );

    if ( $img[0] > $maximum['width'] )
        $file['error'] = 
            'Image too large. Maximum width is ' 
            . $maximum['width'] 
            . 'px. Uploaded image width is ' 
            . $img[0] . 'px';

    elseif ( $img[1] > $maximum['height'] )
        $file['error'] = 
            'Image too large. Maximum height is ' 
            . $maximum['height'] 
            . 'px. Uploaded image height is ' 
            . $img[1] . 'px';

    return $file;
}
2
  • Thanks, I'll check this out and get back asap. Jul 30, 2020 at 12:00
  • Hi @clayRay, so I tried your suggestion. It may have been working in the background but it didn't return and error to the frontend relating to the size of the image instead is said that the server was busy. It probably timed out trying to downscale the image when it detected is was more than the maximum specified. I've updated my post to show what I've done for review. Aug 2, 2020 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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