2011-09-12 35 views
1

我有3張圖片想在網頁上水平放置,它們的比例各不相同,我希望它們共享一個特定的高度(待計算)。所以,假設我的頁面寬度爲't',圖像的當前尺寸爲h1 x w1,h2 x w2,h3 x w3如何縮放n張圖片以適合某個寬度?

我制定了2張圖片的公式,但無法獲取我的圖片頭部周圍3個或更多:

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

那是什麼'特定高度?我知道這是你想要計算的東西,但我不明白它應該代表什麼。儘可能最大的高度,同時仍然符合't'? – Vache

+0

@Vache是​​的,這是最大的高度仍然適合t –

回答

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) 

我認爲應該是這樣,如果我完全錯了,回覆! :)

+0

謝謝,就是這樣! –

+0

這對我來說很合適。 – Chris

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); 
    } 
} 
相關問題