TransWikia.com

How can I restrict image upload dimensions for non-admin WordPress users?

WordPress Development Asked by Patrick Aziken on January 6, 2022

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;
}

One Answer

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;
}

Answered by clayRay on January 6, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP