I have grabbed this function and adjusted it to my needs.
add_filter( 'wp_handle_upload_prefilter', 'check_image_dimensions_during_upload' );
function check_image_dimensions_during_upload( $file ){
$mimes = array( 'image/jpeg', 'image/png', 'image/gif' );
if( !in_array( $file['type'], $mimes ) )
return $file;
$img = getimagesize( $file['tmp_name'] );
$maximum = array( 'width' => 2500, 'height' => 1000 );
$minimum = array( 'width' => 1600, 'height' => 500 );
// Maximums
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';
// Minimums
elseif ( $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;
}
It works like a charm for all image media uploads (that don't match the $maximum or $minimum) but with one strange anomaly?
Steps to reproduce
Choose the 'Set featured image' option from any post or page metabox to reveal the Wordpress media dialog popup.
Choose "Upload Files" tab and either "drag" or "browse" for an image (that does not match the $maximum or $minimum).
The issue
The error message is shown as expected when the image dimensions do not match. However, the generated media thumbnail with progress bar (for the failed image) remains after the error message is dismissed? (See Image Below)
This does NOT occur when uploading images via the Media Library or in the post content "Add media" options?
I see no console errors and nothing in my php error log?
Q) Does anyone know why this could be occurring? Am I missing a filter for the featured image uploading process?
Any help appreciated :-)