As our users regularly upload ~6MB images to use on the website (and aren’t too familiar with how to resize them first), WordPress stores the originals as well as resizing them to several different sizes.
I’d like a function or plugin that takes the uploaded image, resizes it down to something more manageable and then replaces the original.
I’ve seen some functions that delete the original but do not replace it, meaning it is impossible to regenerate the thumbnails at a later date. I need this to be replaced so the user can upload a large image and it is automatically resized down and stored for future resizing if needed.
Add this to the functions.php file in the theme folder. It replaces the original image with the large image set in settings. You might want to setup a new image format and use that as the new original size though.
There’s one nasty bug in the solution above. The solution works as a charm for new images, but for old images, you should never compare to
$upload_dir['path']
because that’s the current upload folder of the current month.Replace the following:
May I suggest an update to the code of the above answer? Unfortunately in newer versions of WordPress the key ‘path’ is no longer provided for the file sizes. So to make it work on older post uploads, we should first get the current subdir from the original image and use this to make the location path for the large image.
So replace this line:
by these 2 lines:
I posted this on another very similar question here but thought it’s worth reposting.
I had issues with the code above and what worked for me was essentially changing these lines.:
with:
I posted my full code and more explanation here:
Delete original image – keep thumbnail?