Auto-modifying original [full size] images

Is there a way to make WordPress process Full size images, which by default it leaves unmodified?

Related posts

Leave a Reply

1 comment

  1. Yes, there is.

    Found here:
    http://www.wprecipes.com/how-to-automatically-use-resized-image-instead-of-originals

    It will replace the original picture with the large size, defined in Media Settings (/wp-admin/options-media.php).

    Here’s the code:

    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'];
    
        // 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;
    }
    

    It is possible to use it with a custom size:

    add_image_size( 'new-large', 1600, 1200 ); 
    

    Changing all the occurrences of $image_data['sizes']['large'] in the original code with $image_data['sizes']['new-large']