Skip to content

Instantly share code, notes, and snippets.

Created March 12, 2016 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e8ba297ecd4b75099164 to your computer and use it in GitHub Desktop.
Save anonymous/e8ba297ecd4b75099164 to your computer and use it in GitHub Desktop.
check image size for uploading
// check for file upload size //
add_action( 'admin_init', 'wpse_28359_block_vendors_from_uploading_small_images' );
function wpse_28359_block_vendors_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' => 800, 'height' => 450 );
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';
$img = getimagesize( $file['tmp_name'] );
$minimum = array( 'width' => 800, 'height' => 450 );
if( !in_array( $file['type'], $mimes ) )
return $file;
$img = getimagesize( $file['tmp_name'] );
$maximum = array( 'width' => 800, 'height' => 450 );
return $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment