Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active June 5, 2023 17:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjn/5037437 to your computer and use it in GitHub Desktop.
Save tomjn/5037437 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Deny Giant Image Uploads
* Description: Prevents Uploads of images greater than 3.2MP
* Author: TJNowell
* Author URI: http://tomjn.com
* Plugin URI: http://tomjn.com/164/clients-who-upload-huge-camera-photos-decompression-bombs/
* Version: 1.1
*/
function tomjn_deny_giant_images($file){
$type = explode('/',$file['type']);
if($type[0] == 'image'){
list( $width, $height, $imagetype, $hwstring, $mime, $rgb_r_cmyk, $bit ) = getimagesize( $file['tmp_name'] );
if($width * $height > 3200728){ // I added 100,000 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';
}
}
return $file;
}
add_filter('wp_handle_upload_prefilter','tomjn_deny_giant_images');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment