如何限制非管理员WordPress用户的图片上传维度?

时间:2020-07-25 作者:Patrick Aziken

我想将非WordPress管理员上传的图像尺寸限制为特定大小。我还想返回一个特定的错误反馈时,上传的图像不符合要求。

例如:如果我将限制设置为500px的宽度和700px的高度,如果用户尝试上载任何与我指定的宽度和高度不同的内容,它将被拒绝,并返回一条消息,说明图像大小应为500px×700px,仅jpg格式。

我找到了一个只需要最小尺寸的解决方案,但它离我想要的有点远(How to Require a Minimum Image Dimension for Uploading?).

我已添加@clayRay\'s建议,请帮助检查:

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 个回复
SO网友:clayRay

我修改了this answer 来自brasofilo。根据您的需要修改以下代码中以下行中的宽度和高度值。。。

    $maximum = array( \'width\' => 500, \'height\' => 700 );
。。。然后将代码添加到主题的函数中。php文件。

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

相关推荐

Fix hover images blink

我使用php生成了这个css脚本,它用于在用户悬停时显示wordpress的第二个特色图像。问题是图像在闪烁,我不知道如何解决这个小问题。有人能建议解决这个问题吗?我已经实现了lazyload,但在这种情况下它是无用的。<div class=\"box\"> <?php $id = get_the_ID(); ?> <div class=\"rounded-circle\" style=\"background-image:url(\'