-
-
Save anonymous/e8ba297ecd4b75099164 to your computer and use it in GitHub Desktop.
check image size for uploading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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