Skip to main content
Known Participant
March 11, 2019
解決済み

Custom icon saving script is exporting PNGs at HUGE sizes?

  • March 11, 2019
  • 返信数 1.
  • 1412 ビュー

Hello, I have a script which saves icons out at various sizes, the problem being is that there is odd behavior exhibited, such as:

A 22px by 22px png at 72dpi with a file size of 12.5Mb

Clearly there is some metadata or some such which must account for the inflated size. There just is not enough "physical" data there to generate that size. The other explanation is that my script is just all jacked up. If anyone could take a look and maybe see where I have gone wrong, I would appreciate it.

Entire .jsx:

#target 'photoshop'

//Array of desired resolutions

var resolutions = [1024, 912, 512, 152, 120, 114, 80, 76, 58, 50, 44, 40, 29, 25, 22];

//Variable reference to the current opened photoshop file

var doc = app.activeDocument;

var docPath = File(app.activeDocument.path + '/' + app.activeDocument.name);

//Create the GUI window

var appWindow = new Window('dialog', 'Icon Processor (ver. 1.0)');

//Styling the window

//Size (must use an window.onShow(){}; callback

appWindow.onShow = function(){appWindow.size = 'width: 230, height: 300';}

//The Create Directory input

var createDir = appWindow.add('statictext',undefined,'Create Folder Name:');

//Input textfield

var dirNameInpt = appWindow.add('edittext',undefined,undefined);

dirNameInpt.characters = 20;

dirNameInpt.active = true;

//var dirName = dirNameInpt.text;

//The Create Directory input

var createFile = appWindow.add('statictext',undefined,'Enter File Name:');

//Input textfield

var fNameInpt = appWindow.add('edittext',undefined,undefined);

fNameInpt.characters = 20;

//var fileName = fNameInpt.text;

//The execute button

var execBtn = appWindow.add('button',undefined,'Execute');

execBtn.onClick = function()

{

      

//Loop through the array of resolutions

for(var i=0; i<resolutions.length; i++)

{

//Resize the image by each resolution in the array set dpi to 72

doc.resizeImage(UnitValue(resolutions, 'px'), UnitValue(resolutions, 'px'), 72, ResampleMethod.BICUBIC, 100);

           

//Call the export PNG function for each resolution pass in the doc and name of the file as parameters

exPNG24(fNameInpt.text, dirNameInpt.text, resolutions);

           

            closeFile();

            openFile();

}

       

        //Close the application window

        appWindow.close();

       

};

//Close button

    var closeBtn = appWindow.add('button',undefined,'Cancel');

    closeBtn.onClick = function()

    {

        appWindow.close();

    };

//Display the window

appWindow.show();

function closeFile()

{

    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

function openFile()

{

    app.open(docPath);

    doc = app.activeDocument;

}

function exPNG24(f,d,i)

    {

        var name = f;

var directory = d;

var i = i;

       

        //Write a new file appending the file name with the resolution of the image

        var folder = new Folder(doc.path + '/' + directory);

        if(!folder.exists);

        folder.create();

        var file = new File(folder + '/' + name + '_' + i + '.png');

       

        //These are the options for saving the PNGs

        var options = new PNGSaveOptions();

        options.format = SaveDocumentType.PNG;

        //Use PNG-24

        options.PNG8 = false;

        //Preserve transparency

        options.transparency = true;

        options.interlaced = false;

        options.embedColorProfile = false;

        //Maximum quality and medium compression to keep file size low

        options.quality = 100;

        options.compression = 5;

       

        //remove meta data

        doc.info = null;

        //Save the image

        doc.saveAs(file, options, true, Extension.LOWERCASE);

       // doc.exportDocument(file, ExportType.SAVEFORWEB, options);

       

    }

//This doesn't work yet but not important as the window can be manually closed by pressing 'cancel'

appWindow.close();

このトピックへの返信は締め切られました。
解決に役立った回答 Stephen Marsh

Thanks! I'm scouring the scripting guide currently! Thanks for the info!


You would be looking for entries similar to (not the full code, just snippets):

exportOptions = new ExportOptionsSaveForWeb();

exportOptions.format = SaveDocumentType.PNG;

exportOptions.PNG8 = false; // false = PNG-24

exportOptions.transparency = true; // true = transparent

exportOptions.interlaced = false; // true = interlacing on

exportOptions.includeProfile = true; // false = don't embedd ICC profile

document.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);

document.close(SaveOptions.DONOTSAVECHANGES);

or

function getPNGOptions()

{

   

    // Create the PDFSaveOptions object to set the PDF options

    var pngExportOpts = new ExportOptionsPNG24();

       

    // Setting PNGExportOptions properties. Please see the JavaScript Reference

    // for a description of these properties.

    // Add more properties here if you like

    pngExportOpts.antiAliasing = true;

    pngExportOpts.artBoardClipping = false;

    //pngExportOpts.horizontalScale = 100.0;

    //pngExportOpts.matte = true;

    //pngExportOpts.matteColor = 0, 0, 0;

    pngExportOpts.saveAsHTML = false;

    pngExportOpts.transparency = true;

    //pngExportOpts.verticalScale = 100.0;

    return pngExportOpts;

}

or

function SaveForWeb() {

    var sfwOptions = new ExportOptionsSaveForWeb();

    sfwOptions.format = SaveDocumentType.PNG;

    sfwOptions.PNG8 = false;

    sfwOptions.interlaced = false;

    sfwOptions.transparency = true;

    sfwOptions.includeProfile = true;

    //sfwOptions.matte = true;

    //sfwOptions.matteColor = 0, 0, 0;

    activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);

   

};

返信数 1

Stephen Marsh
Community Expert
Community Expert
March 11, 2019

I’m guessing that it is the following issue:

Prepression: Metadata Bloat – photoshop:DocumentAncestors

You are using “save as” for the PNG, however if you changed the code to use export/save for web such metadata would be stripped from the export (if that is the actual problem in the first place).

dayglowdave作成者
Known Participant
March 11, 2019

Thanks, I will dig into it. I haven't heard of that before. kind of new to scripting in photoshop.

Stephen Marsh
Community Expert
Community Expert
March 12, 2019

The metadata issue is not specific to scripting.

You will see script code to remove the metadata in my link, so perhaps you could graft that onto your existing code and achieve smaller file sizes (if that is the issue)… Or just search for PNG save for web javascript code, there is heaps out there as well as being in the offical JavaScript reference. Let me know if you need a sample. I’m new to scripting too.