Jump to content

Batch resizing images via CLI - non ProcessWire


Pete
 Share

Recommended Posts

Hey folks, so in a project I'm working on there are 750+ news articles in WP that are image-heavy in recent years. WP has a habit of storing the original images full-size - well, ProcessWire does too but I always set max dimensions on the image fields to resize during upload to prevent 2mb+ images on my disk. This had resulted in an uploads folder some 25GB in size from 2003-2022.

Disk space is, fortunately, cheap nowadays, however I soon realized after mulling it over with Ryan that the easiest and most sane option for importing all those articles was to scrape in the post content HTML into a PW CKEditor field (thanks SimpleHTMLDOM for making this so simple!) but leave the images alone - WP had already done resizes for various lightboxes and galleries so it was just the original, huge images I had to contend with.

The solution - on my own copy of the WP uploads dir - was to run this command and watch it go:

find ./ -type f \( -iname \*.jpg -o -iname \*.jpeg -o -iname \*.png \) -exec mogrify -quality 90 -verbose -resize 1600x1600\> {} \;

It's basically searching all files and subfolders for .jpg, .jpeg and .png files and resizing to no more than 1600px landscape/portrait whilst preserving aspect ratio (no cropping) and 90% image quality. I chose those sizes as the various links in galleries and lightboxes were loading the original file, not one of the WP resized ones, so now we can be fairly sure that we're loading <500kb of image maximum instead of sometimes 10mb+ (some folks have really nice cameras 🙂 ).

I'm sure @horst can probably tell me if that command could be better. I think mogrify is usually for a batch of files for example, whereas the find command is serving them up individually so convert might be more appropriate than mogrify, but this command iterates through the files quickly and only resizes which ones it needs to and is happily chugging away right now so I'm happy with it.

One thing to note is that any resizing can be a bit CPU-heavy, so if you have the chance to do this on a local server first or out of "normal" hours for site visitors then that's recommended as the server may slow down for the duration.

  • Like 7
Link to comment
Share on other sites

One other thing - I am also keeping a copy of the original uploads folder just in case. I've set max resolutions in various CMS' since 2001 and in later years you wonder why you ever set it as low as 800x800 for example 😄 This way I have the originals if I ever need them again, but I think for the content in question, 1600px max width/height should be absolutely fine.

There will also likely be a write-up for this project - it's going to be a while longer though before it's ready.

  • Like 2
Link to comment
Share on other sites

  • Pete changed the title to Batch resizing images via CLI - non ProcessWire

Thanks for sharing! We have taken over a trainwreck of a Craft site from another agency recently, and amongst a million other nuisances, the image resizer plugin they've chosen is end-of-life. Plus, the authors are fond of uploading huge images. I just might steal that line of code to bring the overall size down considerably...

  • Like 2
Link to comment
Share on other sites

Go for it - just be aware that you don't have the originals afterwards of course.

You've reminded me I also have a Craft site I can run this on, although the client does usually manually resize their own images it would be good to check.

  • Like 2
Link to comment
Share on other sites

On 6/5/2022 at 12:12 PM, Pete said:

I'm sure @horst can probably tell me if that command could be better. I think mogrify is usually for a batch of files for example, whereas the find command is serving them up individually so convert might be more appropriate than mogrify, but this command iterates through the files quickly and only resizes which ones it needs to and is happily chugging away right now so I'm happy with it.

Hey @Pete, I do not see any possible improvements with your code.
(So this also may be a bit because my CLI knowledge of this tools maybe a bit lower as you expect. 😁 - On my local machine I (can & do) use other tools for those things. 🙂)

 

  • Like 2
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...