如何缩放n个图像以适合某个宽度?

时间:2011-09-12 15:51:27

标签: image math algebra

我想要在网页上并排放置3张图像,它们的比例各不相同,我希望它们最终共享一个特定的高度(待计算)。所以我要说我的页面宽度为't',图像的当前尺寸为h1 x w1,h2 x w2,h3 x w3

我为2张图片制定了一个公式,但我无法理解3张或更多图片:

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

2 个答案:

答案 0 :(得分:4)

你必须尊重的条件是:

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

其中kn是应用于宽度的缩放常量,以保持图像的原始比例具有新的高度。

我们可以说

kn = h_new / hn

其中h_new是所有图片的新高度。从那里它是所有替代和隔离

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)

我认为应该是这样,如果我完全错了,请回复! :)

答案 1 :(得分:1)

我在javascript中编写了一个photoshop CS5脚本来调整大小,并根据@ vache的公式保存打开的图像。希望有人发现它有用:

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);
    }
}
相关问题