Update: This functionality was incorporated into WP-CLI, the WP-CLI team took this code and used it in the main program.
A site I am working on has a unique problem, where we need to batch re-size all existing media attachments, in order for all the previous pictures to fit into the new template. Although this problem in itself isn’t rare or unique, the fact that there were just over 45,000 images in their library that needed to be regenerated made it much harder than usual.
Normally, in this situation I’d just use the Regenerate Thumbnails plugin, and that’d be that. Because there were so many images to resize, and the way this and other similar plugins work, is by making sequential ajax requests, meant that I’d have to leave a browser window open for a day or two – assuming it didn’t crash due to the size of the DOM.
What I did to get around this, is take the Regenerate Thumbnails plugin, and re-build so it could be used via the command line of the server, meaning it’d be much faster and more efficient!
The hard work was done by the WP-CLI guys, so all I had to do was re-build the Regenerate Thumbnails plugin, and we were off.
Some extra problems that I encountered along the way, and how I fixed them:
Memory limits – every once in a while, either because it was a huge upload, or we’d been running long enough, I’d get php memory max out errors. To get around this, I started to check if one of the new post image sizes existed – if it did, we didn’t have to re-size the image again.
Old file paths – WordPress stores the original file path with the absolute URL. The site I was working on had moved 4-5 times over it’s life, so there were lots of “cant find file” errors. So I had to string replace an array of old file paths
My most recent version is below, I’ll try adopt some of the changes and put a nice up-to-date version on GitHub
I’m not sure if there would have been a better way to do this, I wasn’t able to find anything floating around the internet – so I thought I’d share how I did it here!