最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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

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;
}
Share Improve this question edited Aug 2, 2020 at 19:18 Patrick Aziken asked Jul 25, 2020 at 1:00 Patrick AzikenPatrick Aziken 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
}
发布评论

评论列表(0)

  1. 暂无评论