No More Large Images

How to prevent the upload of large images to Wordpress Media library

One of the biggest concerns when building a website is to keep the file sizes down to a minimum. This is particularly relevant when uploading images. Clients will rarely have experience with sizing images, let alone optimising them for online use. I've frequently seen users uploading images of up to 7MB, and then they wonder why the page load is slow.

The script below can be uploaded as a Wordpress plugin - simply save as a php file and upload to the plugins folder. Or, even better, create a new folder in /wp-content, called mu-plugins (Must Use plugins). Plugins saved in the mu-plugins folder cannot be disabled from within Wordpress.

You can adjust the size restrictions by editing lines 20, 24, and 25 (as highlighted). You can also change the error message that will be displayed by editing lines 31 and 33.

<?php
/*
 * Plugin Name: Image Upload Size Restrictions
 * Description: Prevents the upload of ridiculously large images to the web server
 * Version: 1.1
 * Author: Titus
 * Author URI: https://titus-design.com
 */

//if (!defined('ABSPATH')) die('Restricted Area');



add_filter('wp_handle_upload_prefilter','mdu_validate_image_size');
function mdu_validate_image_size( $file ) {

	// Calculate the image size in KB
	$image_size = $file['size']/1024;
	// File size limit in KB
	$limit = 500;

    $image = getimagesize($file['tmp_name']);
    $maximum = array(
        'width' => '2000',
        'height' => '2000'
    );
    $image_width = $image[0];
    $image_height = $image[1];


    $too_big = "Image file size is too big. It needs to be smaller than  $limit KB. Please save photos as jpg no larger than {$maximum['width']} by {$maximum['height']} pixels.";

    $too_large = "Image dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";
    

    // Check if it's an image
	$is_image = strpos($file['type'], 'image');

    if ( ( $image_size > $limit ) && ($is_image !== false) ) {
        //add in the field 'error' of the $file array the message
        $file['error'] = $too_big; 
        return $file;
    }
    elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
        //add in the field 'error' of the $file array the message
        $file['error'] = $too_large; 
        return $file;
    }
    else
        return $file;
}


?>

Credit - The original source for this came from Marc Dingena. I've modified this to include a restriction on file size, so that even if the image is 2000x2000px, it will still throw an error message if it is larger than 500kb - obviously, you can amend the limits to whatever you like.

Get in touch...

Titus Design

4 Apperley Park, Apperley, Gloucester, GL19 4EB
+44 (0)1452 780621

Specialising in responsive Wordpress website design, development, hosting, site optimisation, digital marketing, as well as graphic design for print.

We recommend
email anti spam software
by MX GuardDog
cookie settings