Limit image resolution on upload

I’m running into a problem where a particularly huge image is causing my VPS server to reboot because of memory issues. The configuration of this is out of my hands, it’s sort of like the host’s failsafe.

I have a VPS with 300M memory, which I believe is sufficient for my WordPress installation, at least with day-to-day operations.

Read More

The image I am uploading isn’t particularly big in file size (~380 kb) and is mostly black with white letters. However, its resolution is 4583 * 4583. When I upload it through WordPress, I get an HTTP error in the upload window, and when I go check the live site, it’s down for a couple of minutes.

I was wondering if it’s possible to detect an images resolution and prevent it from being uploaded. I’m clever enough to mess around a bit in an image processor, but some of my authors may not be savvy enough to process the image before they upload something as big as this.

It would be even better if the limitation could be based on the images bits-per-pixel, but I have no idea what my server’s limit is for this, so I guess an error message saying the image can’t be uploaded because it’s larger than, say, 2000 * 2000 is enough.

Related posts

3 comments

  1. The problem isn’t so much the uploading itself, as that is a network connection between the client and the server. It’s not what’s eating the server’s memory.

    When WordPress starts ‘cruncing’ the images, that is where PHP comes in and start resizing and cropping the uploaded images. It is before this moment you need to step in and perform a couple of checks before allowing PHP to eat up your memory.

    Using the wp_handle_upload_prefilter filter, you can hook a function that performs any check you’d like on the image that is about to be crunched:

    <?php 
    /* Marc Dingena Utilities
     * Test image resolution before image crunch
     */
    add_filter('wp_handle_upload_prefilter','mdu_validate_image_size');
    function mdu_validate_image_size( $file ) {
        $image = getimagesize($file['tmp_name']);
        $minimum = array(
            'width' => '400',
            'height' => '400'
        );
        $maximum = array(
            'width' => '2000',
            'height' => '2000'
        );
        $image_width = $image[0];
        $image_height = $image[1];
    
        $too_small = "Image dimensions are too small. Minimum size is {$minimum['width']} by {$minimum['height']} pixels. Uploaded image is $image_width by $image_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.";
    
        if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
            // add in the field 'error' of the $file array the message 
            $file['error'] = $too_small; 
            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;
    }
    ?>
    
  2. For a plugin, try http://wordpress.org/plugins/imsanity/ and that will help with keeping other site users from uploading images that are too large.

    Imsanity automatically resizes huge image uploads down to a size that
    is more reasonable for display in browser, yet still more than large
    enough for typical website use. The plugin is configurable with a max
    width, height and quality. When a contributor uploads an image that is
    larger than the configured size, Imsanity will automatically scale it
    down to the configured size and replace the original image.

  3. It’s possible that your server has enough memory to do the task, but that the PHP memory limit setting is just too low for the job of converting very large images.

    Try setting memory_limit in php.ini higher.

    memory_limit=256M
    

    *Note: This might not be an appropriate solution in some hosting environments.

Comments are closed.