Batch resizing 45,000 images in WordPress

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!

 

Ben May

I run a small WordPress development agency, specialising in enterprise and high end WordPress engineering challenges. I love good coffee, fine food and wine, and working on the web.

5 thoughts on “Batch resizing 45,000 images in WordPress

  1. I want to regenerate thumbnails and tried Regenerate Thumbnail plugin but it is not fast as much as I want.While surfing internet I found your solution but I don’t have idea how to use your solution.Please if you can help me to regenerate thumbnails more than 80,000, I will be thankful.
    How to use….please

    1. Hi Rohan,

      This code is now built into WP-CLI. So if you just get WP-CLI up and running, you can do it all through the commandline without having to install the above code.

      Best of luck, Ben.

      1. Hi Ben !
        Thank you for quick response.I am not familiar with command line so I found ForcetoRegenerateThumbnail plugin but it’s very difficult to regenerate 75000 thumbnails on image based blog.
        Any simple and fast method ? Please I need your support.
        I have installed WP-Cli Plugin but I don’t know how it works because this plugin haven’t any setting panel.
        Waiting for your response.
        Thanking You.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>