Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress removing original image after resize?

In wordpress media library is there any way to remove original image after a resize? It seems to keep the original and I feel this wastes a lot of space.

like image 296
Jonny Jordan Avatar asked Jan 13 '17 01:01

Jonny Jordan


2 Answers

You have to use wp_generate_attachment_metadata filter to manipulate upload image.

Here is the code:

add_filter('wp_generate_attachment_metadata', 'txt_domain_delete_fullsize_image');

function txt_domain_delete_fullsize_image($metadata)
{
    $upload_dir = wp_upload_dir();
    $full_image_path = trailingslashit($upload_dir['basedir']) . $metadata['file'];
    $deleted = unlink($full_image_path);

    return $metadata;
}

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
The code is tested and fully functional.
Hope this helps!

like image 199
Raunak Gupta Avatar answered Oct 31 '22 20:10

Raunak Gupta


I don't know the direct answer to this and it's a good idea to keep the original image for internal wordpress plugins.

However one way you can reduce the storage and reduce cost is to use the AWS S3 Plugin: https://wordpress.org/plugins/amazon-s3-and-cloudfront/ This will require you to first setup an AWS S3 Bucket. Feel free to ask another question related to doing that if it doesn't make sense.

like image 20
Ben Fellows Avatar answered Oct 31 '22 19:10

Ben Fellows