Mark Robinson
Mark Robinson

Reputation: 1629

How do you scale n images to fit a certain width?

I've got 3 images I want to fit on a web page horizontally side by side, they are of various proportions and I want them to end up sharing a particular height (to be calculated). So let's say the width of my page is 't' and the current dimensions of the images are h1 x w1, h2 x w2, h3 x w3

I worked out a formula for 2 images but I can't get my head around 3 or more:

(h1*h2*t) / (w1*h2 + h1*w2)

Upvotes: 1

Views: 159

Answers (2)

Mark Robinson
Mark Robinson

Reputation: 1629

I wrote a photoshop CS5 script in javascript to resize and save out the open images based on @vache's formula. Hope someone finds it useful:

var outputFolder = Folder.selectDialog("Select a folder for the output files")

if(outputFolder != null) {
    var startRulerUnits = app.preferences.rulerUnits 
    var startDisplayDialogs = app.displayDialogs
    // Set Adobe Photoshop CS5 to use pixels and display no dialogs 
    app.preferences.rulerUnits = Units.PIXELS 
    app.displayDialogs = DialogModes.NO

    do {
        var totalWidth = parseInt( prompt("How wide do they need to fit into?", 844) );
    }
    while(totalWidth <= 0 || isNaN(totalWidth));
    var DL = documents.length;
    var totalArea = 0;

    for(a=0;a<DL;a++){
        var cur = documents[a];
        totalArea += cur.width / cur.height;
    }
    var newHeight = totalWidth / totalArea;

    for(a=1;a<=DL;a++){

        activeDocument = documents[a-1];
        var AD=activeDocument;
        // bring to front
        app.activeDocument = AD;
        AD.changeMode(ChangeMode.RGB);
        var imgName= AD.name.toLowerCase();
        imgName = imgName.substr(0, imgName.length -4);
        AD.flatten();

        AD.resizeImage(null,UnitValue(newHeight,"px"),null,ResampleMethod.BICUBIC);
        //AD.resizeImage(UnitValue(newWidth,"px"),null,null,ResampleMethod.BICUBIC);

        saveForWeb(outputFolder, imgName, AD);              
    }

    // Close all the open documents 
    while (app.documents.length) {
       app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
    }
    // Reset the application preferences 
    app.preferences.rulerUnits = startRulerUnits;
    app.displayDialogs = startDisplayDialogs;
}

function saveForWeb(outputFolderStr, filename, AD)
{
    var opts, file;
    opts = new ExportOptionsSaveForWeb();
    opts.format = SaveDocumentType.JPEG;
    opts.quality = 80;
    if (filename.length > 27) {
        file = new File(outputFolderStr + "/temp.jpg");
        AD.exportDocument(file, ExportType.SAVEFORWEB, opts);
        file.rename(filename + ".jpg");
    }
    else {
        file = new File(outputFolderStr + "/" + filename + ".jpg");
        AD.exportDocument(file, ExportType.SAVEFORWEB, opts);
    }
}

Upvotes: 1

dee-see
dee-see

Reputation: 24088

The condition you must respect is:

k1*w1 + k2*w2 + ... + kn*wn = t

where kn is the scaling constant applied to the width to keep the original proportion of the image with its new height.

We can say that

kn = h_new / hn

where h_new is the new height for all images. From there it's all substitution and isolation

h_new*w1/h1 + h_new*w2/h2 + ... + h_new*wn/hn = t
h_new * (w1/h1 + w2/h2 + ... + wn/hn) = t
h_new = t / (w1/h1 + w2/h2 + ... + wn/hn)

I think that should be it, reply if I'm completely wrong! :)

Upvotes: 4

Related Questions