Skip to main content
Known Participant
July 17, 2026
Open for Voting

Automatically resizing resolution of the image to set file size in Export As

  • July 17, 2026
  • 4 replies
  • 68 views

Basically idea is when you set, let’s just say 100 megabyte to file size, export as automatically resize the image according to ratio of the image. This feature would be a really nice time saver.

    4 replies

    Stephen Marsh
    Community Expert
    Community Expert
    July 28, 2026

    Outside of Adobe programming something, options are limited. The following script is the best that I can offer for now.

     

     

    What it does:

    Instead of the more common approach of lowering JPEG quality to hit a target size, this script keeps the JPEG quality fixed (90%) and reduces pixel dimensions instead. You may wish to change the 90% value, there is a balance between the quality level and the pixel width/height to achieve a target file size on drive.

    • Takes the currently open document and exports it via "Save for Web" as a JPEG at a fixed quality (90).
    • If the resulting file is larger than the target size, it deletes that file, shrinks the pixel dimensions by a step percentage (default 10%), and tries again.
    • This continues until the file fits under the target size, or until the scale hits a safety floor (default 25% of original dimensions), at which point it gives up and warns the user.
    • Each pass is done on a duplicate of the original document, so the original stays untouched, and every resize step scales from the original dimensions rather than compounding the resampling of the previously resized copy.
    • Output files are saved with dimensions in the filename (myImage_1200x800px.jpg).

     

    Two modes:

    • Interactive (HEADLESS_MODE = false): shows a dialog letting the user set/confirm the target size (KB) and the resize step (%) before running.
    • Headless (HEADLESS_MODE = true): skips the dialog and just uses the hardcoded MaxSz value. Intended for batch action automation.

     

    /*
    JPEG Resize to Target Size KB
    Resizes the pixel dimensions of a saved copy to achieve a targeted file size on drive.
    Stephen Marsh
    24th July 2026 - v1.0: Initial release
    https://community.adobe.com/feature-requests-713/automatically-resizing-resolution-of-the-image-to-set-file-size-in-export-as-1632557
    Based on:
    https://community.adobe.com/questions-712/slow-operation-of-the-script-1149419
    */

    ////////////////////
    // Set HEADLESS_MODE to true to run without showing the target-size dialog, which is
    // useful for batch/automation. When set to true, the script uses MaxSz (as set below)
    // as the target size in bytes and skips the dialog.
    //
    // When HEADLESS_MODE is set to false, the dialog is shown so the user can confirm/override
    // the target size interactively.
    ////////////////////
    // User script execution Preference
    var HEADLESS_MODE = false;
    ////////////////////
    // Set the target size, i.e. 500000 = max. bytes (500Kb)
    var MaxSz = 500000; // Also used as the headless target size, change as needed!
    ////////////////////

    // Check that a document is open before proceeding
    if (app.documents.length === 0) {
    alert("No document is open! Please open a document and try again.");
    throw new Error("__CANCEL__");
    }

    main();

    function main() {
    var docRef = activeDocument;
    var outputFolder = docRef.path;
    var currentNameWithoutExtension = docRef.name.replace(/\.[^\.]+$/, '');

    // Points at the most recently saved file on disk, used both to check the resulting file size
    // and to remove that file if it misses the target, before the next (smaller) pass is saved
    // under its own new name.
    var NewfileRef = null;

    ////////////////////
    var qualityFactor = 90; // Fixed JPEG save quality value (quality is no longer reduced - pixel dimensions are reduced instead)
    var resizePct = 10; // Decreasing size step % value (percentage points knocked off the scale each pass)
    var minScalePct = 25; // Safety floor - stop reducing once the scale would drop to/below this % of the original
    var theResampleMethod = ResampleMethod.BICUBICSHARPER; // ResampleMethod.BICUBIC || ResampleMethod.BICUBICSMOOTHER
    ////////////////////

    // Capture the ORIGINAL pixel dimensions once, up front, before any resizing takes place.
    // Every pass below resizes a fresh duplicate from the original image
    var origWidthPx = docRef.width.as("px");
    var origHeightPx = docRef.height.as("px");
    var origResolution = docRef.resolution;

    var iterations = 0; // Tracks the number of Save for Web export passes
    var scalePct = 100; // Current % of the ORIGINAL pixel dimensions used for this pass
    var tempDoc = null; // The disposable, resized duplicate used for exporting

    // Suppress dialogs
    var origDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    try {
    if (HEADLESS_MODE) {
    // Headless: skip the dialog entirely, use MaxSz as-is (already in bytes).
    }
    else {
    // Show the dialog to let the user set/confirm the target size (in KB)
    // and the resize step (%) used between passes
    var dialogResult = showTargetSizeDialog(MaxSz, resizePct);

    if (dialogResult === null) {
    // User cancelled
    return;
    }

    MaxSz = dialogResult.maxSizeBytes; // MaxSz is stored in bytes
    resizePct = dialogResult.stepPct;
    }

    // Perform the first Save for Web pass at the original pixel dimensions, on a duplicate
    var pass = duplicateAndResize(scalePct);
    tempDoc = pass.doc;
    NewfileRef = buildFileRef(pass.width, pass.height);
    s4wJPEG(tempDoc, NewfileRef, qualityFactor);
    tempDoc.close(SaveOptions.DONOTSAVECHANGES);
    iterations++;

    // Keep trying to save under MaxSz by shrinking the pixel dimensions
    while (NewfileRef.length > MaxSz) {
    scalePct = scalePct - resizePct;

    // If this exceeds the target size - remove it from disk before saving the next,
    // smaller pass under its own new name.
    NewfileRef.remove();

    pass = duplicateAndResize(scalePct);
    tempDoc = pass.doc;
    NewfileRef = buildFileRef(pass.width, pass.height);
    s4wJPEG(tempDoc, NewfileRef, qualityFactor);
    tempDoc.close(SaveOptions.DONOTSAVECHANGES);
    iterations++;

    // The floor
    if (scalePct <= minScalePct) {
    alert("The file can't be saved within the desired size at a reasonable pixel dimension (" + minScalePct + "% of the original).");
    break; // break the loop whenever the scale has shrunk to the safety floor
    }
    }

    // End of script notification
    if (!HEADLESS_MODE) {
    app.beep();
    alert("Saved File: " + NewfileRef.name + " \r " +
    "Save Iterations: " + iterations + " \r " +
    "Final Scale: " + scalePct + "% of original \r " +
    "File Size: " + Math.round(NewfileRef.length / 1024) + " KB"); // Matches Windows Explorer
    }
    }
    finally {
    // Restore the user's original dialog-display preference
    app.displayDialogs = origDisplayDialogs;
    }


    ///// Functions /////

    function duplicateAndResize(pct) {
    var dup = docRef.duplicate();
    var w, h;

    if (pct < 100) {
    w = Math.round(origWidthPx * (pct / 100));
    h = Math.round(origHeightPx * (pct / 100));
    dup.resizeImage(UnitValue(pct, "%"), UnitValue(pct, "%"), origResolution, theResampleMethod);
    }
    else {
    w = Math.round(origWidthPx);
    h = Math.round(origHeightPx);
    }

    return { doc: dup, width: w, height: h };
    }

    function buildFileRef(w, h) {
    return new File(outputFolder + "/" + currentNameWithoutExtension + "_" + w + "x" + h + "px.jpg");
    }

    function s4wJPEG(sourceDoc, FileNm, qualityFactor) {
    var options = new ExportOptionsSaveForWeb();
    options.includeProfile = true;
    options.quality = qualityFactor;
    options.format = SaveDocumentType.JPEG; // Save Format for the file
    sourceDoc.exportDocument(File(FileNm), ExportType.SAVEFORWEB, options);
    }
    }

    function showTargetSizeDialog(defaultSzBytes, defaultStepPct) {
    var defaultSzKB = Math.round(defaultSzBytes / 1000); // Not 1024!?

    // ScriptUI
    var win = new Window("dialog", "JPEG Resize to Target Size (KB) - v1.0");
    win.orientation = "column";
    win.alignChildren = ["fill", "top"];
    win.spacing = 10;
    win.margins = 16;
    win.preferredSize.width = 250;

    // Panel containing the target size and resize step entry fields
    var panel = win.add("panel", undefined, "");
    panel.orientation = "column";
    panel.alignChildren = ["fill", "top"];
    panel.margins = [15, 20, 15, 15];
    panel.spacing = 10;

    var sizeRow = panel.add("group");
    sizeRow.orientation = "row";
    sizeRow.alignChildren = ["left", "center"];
    sizeRow.add("statictext", undefined, "Max size (KB):");
    var szInput = sizeRow.add("editnumber", undefined, String(defaultSzKB));
    szInput.characters = 5;
    szInput.alignment = ["fill", "center"];

    var stepRow = panel.add("group");
    stepRow.orientation = "row";
    stepRow.alignChildren = ["left", "center"];
    stepRow.add("statictext", undefined, "Resize step (%):");
    var stepInput = stepRow.add("editnumber", undefined, String(defaultStepPct));
    stepInput.characters = 5;
    stepInput.alignment = ["fill", "center"];

    // Button row, right aligned, outside/below the panel
    var btnGroup = win.add("group");
    btnGroup.orientation = "row";
    btnGroup.alignment = ["right", "top"];
    btnGroup.spacing = 10;

    var cancelBtn = btnGroup.add("button", undefined, "Cancel", { name: "cancel" });
    var okBtn = btnGroup.add("button", undefined, "OK", { name: "ok" });

    var result = null;

    okBtn.onClick = function () {
    var val = parseFloat(szInput.text);
    if (isNaN(val) || val <= 0) {
    alert("Please enter a valid target size in KB (a positive number).");
    return;
    }

    var stepVal = parseFloat(stepInput.text);
    if (isNaN(stepVal) || stepVal <= 0 || stepVal >= 100) {
    alert("Please enter a valid resize step (a number greater than 0 and less than 100).");
    return;
    }

    result = {
    maxSizeBytes: Math.round(val * 1024), // convert KB back to bytes
    stepPct: stepVal
    };
    win.close();
    };

    cancelBtn.onClick = function () {
    result = null;
    win.close();
    };

    win.show();

    return result;
    }

     

    1. Copy the code text to the clipboard
    2. Open a new blank file in a plain-text editor (not in a word processor)
    3. Paste the code in
    4. Save as a plain text format file – .txt
    5. Rename the saved file extension from .txt to .jsx
    6. Install or browse to the .jsx file to run (see below)

    https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

    Stephen Marsh
    Community Expert
    Community Expert
    July 22, 2026

    What exactly are you asking for? A new option to set a target file size on drive?

     

    To clarify, you want to hit a certain file size on drive, using the current compression level, by proportionally changing the pixel dimensions?

     

    The preference is for file size, by reducing image size?

    miidesignAuthor
    Known Participant
    July 23, 2026

    Yep. I want photoshop to recalculate the image size by file size. And with using multiple export option of “Export As”, this can save so much time.

    Stephen Marsh
    Community Expert
    Community Expert
    July 23, 2026

    @miidesign OK, that’s an interesting request and workflow. Device viewing (i.e. content on a web page) oftenhas the pixel dimensions as a fixed size with one edge at a common largest size and the others proportional, with compression being variable, not the opposite that you mention.

     

    How would you use this in Export As?

    • The entire document, all visible layers?
    • Only a single selected layer?
    • All selected layers, each as a separate file?
    • Any and all of the above?

     

    This could be scripted, using either save or save for web (as Export As isn't scriptable).