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.
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!
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.
Works Wordpress 5.6 This one does show the featured 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;
}
add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
© 2022 - 2024 — McMap. All rights reserved.