Sunday, 13 September 2015 05:32

Batch resize images

Lately I've been migrating a wordpress website onto another server and tidying much of it upo for a client. The website used many photo galleries, and the images had been uploaded to the website directly from a digital camera in the highest resolution, in the process bloating the website file size. So I did some research and came across the following solution to resize the images.

The complication was that whilst there were a lot of really huge images, there were also just as many smaller thumbnail sized images. So it was practical to do a batch resize to a specific dimension. I needed a solution that was smart enough to see if the image was really huge and apply the resize action, or if it was a thumbnail and leave it as is. Further to this, another complication arose because of the way wordpress sorts uploads into multiple folders. I wasn't able to run the standard photoshop batch process on the root folder without destroying the folder hierachy,

I came across the following script in the Adobe forums, and I really must credit the poster Paul Riggott as the author oof the script. It worked a treat, solving almost all my issues. The script resizes any image that is bigger that 900px, ignoring those that are smaller than 900px, whilst maintaining the folder scructure.

  1. In Adobe Bridge, navigate to the root folder where the images are stored locally.
  2. Select "View > Show Items From Subfolders" then select all images.
  3. Run the following script in the Adobe ExtendScript Toolkit:
    #target bridge
    var thumbs = app.document.getSelection("jpg");
    alert("Checking "+ thumbs.length + " files");
    var jpgs = 0;
    for(var a in thumbs){
    var md = thumbs[a].synchronousMetadata;
    var t= new Thumbnail(thumbs[a]);
    md.namespace = "";
    var orientation = md.Orientation.replace(/(\w+)(\s+)(.)(\d+)(.)/,"$3$4");
    if(orientation == 'Normal') orientation =0;
    var bm = new BitmapData(thumbs[a].spec);
    bm = bm.rotate(orientation);
    bm = bm.resize(500,BitmapData.bicubicSharper);
    bm.exportTo(new File(thumbs[a].spec),80); //80 is the quality
    alert(jpgs + " JPEGs have been modified");

The next step was to smush all the images. I did this by creating a "Save for web" action in Photoshop. Opening up the images in Bridge and applying the batch action from within Bridge. Unfortunately I wasn't able to apply this to the root folder, and needed to do each folder individually.

Published in Blog