Remove the original image from the server after Wordpress generates the thumbnails

Asked

Viewed 480 times

4

I need to remove the original image that gives rise to Wordpress thumbnails after thumbnails are generated. I have 3 sizes of Thumbs that I use, but to avoid that the server is left with unnecessary files I need to remove the original, which usually comes with more than 4000px in size and more than 2.5MB in size. I searched some contents related to this question but found nothing. Removing the original after thumbnails were generated is possible? And if yes I eat where I can find the answer?

  • The best way would be to disable thumbnails and use a plugin like https://wordpress.org/plugins/ewww-image-optimizer/

2 answers

4


It is possible to do this using the following code:
(source: How to Automatically use resized images Instead of Originals)

add_filter( 'wp_generate_attachment_metadata', 'replace_uploaded_image' );

function replace_uploaded_image($image_data) 
{
    // abortar se não houver versão "large" da imagem
    if ( !isset($image_data['sizes']['large']) ) 
        return $image_data;

    // paths do upload e da imagem grande
    $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'];

    // deletar original
    unlink($uploaded_image_location);

    // renomear a imagem "large"
    rename($large_image_location, $uploaded_image_location);

    // atualizar o metadata da imagem e retornar
    $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 also possible to change the version large by a custom, as seen in Auto-modifying original [full size] images:

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

And then, just change all the occurrences of $image_data['sizes']['large'] for $image_data['sizes']['new-large'].

0

Look... I believe only manually. You can find them either on FTP, in the folder: wp-content/uploads/ or in the Media Library.

However, I see no need to remove the images. The vast majority of hosts usually have more than 5GB of space, which is quite difficult to "fill". :)

  • 1

    If the site also uploads videos or other large files, it will be very easy to fill ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.