I want to reduce the file sizes of user-uploaded images in a directory on my webserver (or on my local machine, and then upload back to my webserver). I want to aggressively optimize files above a certain size (say, 500 KB) by resizing, applying lossy compression, and so on. Most of the files in the directories are small files that are already optimized, so I don't want to touch them.
Background / What I have tried
I have a Drupal 7 site that allows users to upload photos.
The photos are stored in directories per user like this:
Photos can be jpg, png, or gif.
I allow users to upload photos up to 5 MB in size; I do this because many users don't know how to reduce the file size, so I don't want to limit the file size on upload because that might prevent a user from adding a photo if he or she has low technical skill.
However, since the photos on my site are resized (using the Drupal image styles feature) to 800x600 or smaller, there is no need to have any photos at 5 MB... and since I have about 40,000 photos now, the big photos are really taking up a lot of space.
I already downloaded the photos from my server using rsync, ran ImageOptim (I'm on a Mac, but this is basically jpegtran, pngoptim and other libraries [the ones usually bundled with ImageMagick]) to do lossless compression, and then used rsync to send them back to my server. This reduced file sizes by about 20%.
However, searching the files using the advanced searched function of Finder on my Mac, I noticed about 20% of the total space is taken up by about 2% of the files, which are 500 KB or more. So I would like to resize just these files until they are, say, 200 KB or less by resizing their dimensions, applying lossy compression, and so on. I found this article about resizing images on Smashing Magazine which has useful code for ImageMagick, but I don't know how to easily target only the large files on my system.