Answers:
将此代码添加到主题的functions.php文件中,它将限制最小图像尺寸
add_filter('wp_handle_upload_prefilter','tc_handle_upload_prefilter');
function tc_handle_upload_prefilter($file)
{
$img=getimagesize($file['tmp_name']);
$minimum = array('width' => '640', 'height' => '480');
$width= $img[0];
$height =$img[1];
if ($width < $minimum['width'] )
return array("error"=>"Image dimensions are too small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px");
elseif ($height < $minimum['height'])
return array("error"=>"Image dimensions are too small. Minimum height is {$minimum['height']}px. Uploaded image height is $height px");
else
return $file;
}
然后只需更改所需的最小尺寸的数字即可(在我的示例中为640和480)
我不想重新格式化同事的代码。
因此,这几乎与@MaorBarazany的答案相同,但是要检查mime类型,更改file['error']
声明并将函数名称空间更改为此wpse Question ID。
此外,仅对不是管理员的用户进行检查。
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' => 640, 'height' => 480 );
if ( $img[0] < $minimum['width'] )
$file['error'] =
'Image too small. Minimum width is '
. $minimum['width']
. 'px. Uploaded image width is '
. $img[0] . 'px';
elseif ( $img[1] < $minimum['height'] )
$file['error'] =
'Image too small. Minimum height is '
. $minimum['height']
. 'px. Uploaded image height is '
. $img[1] . 'px';
return $file;
}
挂钩的结果: