Automatically resize WordPress images to a maximum width and height upon uploading?

I created a blog where some users can upload images through the WordPress dashboard. The site gets bogged down quickly because the original images are so big. Some users don’t have the knowledge to resize the images themselves before uploading them, and I don’t want to have to resize them manually.

Is there any way I can set a maximum width and height for uploaded images? I don’t even want the original to remain on the website. I want the largest version of the image on the website to match the width and height restrictions I set.

Related posts

Leave a Reply

3 comments

  1. add this code in your theme’s functions.php it will replace the original image with the the re-sized version.

    function replace_uploaded_image($image_data) {
    // if there is no large image : return
    if (!isset($image_data['sizes']['large'])) return $image_data;
    
    // paths to the uploaded image and the large image
    $upload_dir = wp_upload_dir();
    $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];
    $large_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['large']['file'];
    
    // delete the uploaded image
    unlink($uploaded_image_location);
    
    // rename the large image
    rename($large_image_location,$uploaded_image_location);
    
    // update image metadata and return them
    $image_data['width'] = $image_data['sizes']['large']['width'];
    $image_data['height'] = $image_data['sizes']['large']['height'];
    unset($image_data['sizes']['large']);
    
    return $image_data;
    }
    
    add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
    

    Article Source: http://goo.gl/nkszUn

  2. This will work for new image uploads as well as older ones replacing user uploaded large images automatically with your defined Large size from admin panel’s media settings:

    add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
    function replace_uploaded_image($image_data) {
        // if there is no large image : return
        if (!isset($image_data['sizes']['large'])) return $image_data;
    
        // paths to the uploaded image and the large image
        $upload_dir = wp_upload_dir();
        $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];
        // $large_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['large']['file']; // ** This only works for new image uploads - fixed for older images below.
        $current_subdir = substr($image_data['file'],0,strrpos($image_data['file'],"/"));
        $large_image_location = $upload_dir['basedir'] . '/'.$current_subdir.'/'.$image_data['sizes']['large']['file'];
    
        // delete the uploaded image
        unlink($uploaded_image_location);
    
        // rename the large image
        rename($large_image_location,$uploaded_image_location);
    
        // update image metadata and return them
        $image_data['width'] = $image_data['sizes']['large']['width'];
        $image_data['height'] = $image_data['sizes']['large']['height'];
        unset($image_data['sizes']['large']);
    
        return $image_data;
    }