Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Restrict the maximum file size as well as the maximum allowed resolution for images uploaded to WordPress.
<?php
/**
* Plugin Name: (#67107) »kaiser« Restrict file upload via image dimensions
*/
function wpse67107_restrict_upload( $file )
{
$file_data = getimagesize( $file );
// Abort when we can't get any info:
if ( ! $file_data )
return $file;
// Abort when we ain't got an image:
$file_type = array_shift( explode( '/', $file['type'] ) );
if ( 'image' != $file_type )
return $file;
list( $width, $height, $type, $hwstring, $mime, $rgb_r_cmyk, $bit ) = $file_data;
// Add conditions when to abort
if ( 3200728 < $width * $height )
{
// I added 100k as sometimes, there are more rows/columns
// than visible pixels, depending on the format
$file['error'] = 'This image is too large. Resize it prior to uploading, ideally below 3.2MP or 2048x1536 px.';
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'wpse67107_restrict_upload' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment