Copy link to clipboard
Copied
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();
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.DO
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
Thanks, I will dig into it. I haven't heard of that before. kind of new to scripting in photoshop.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks! I'm scouring the scripting guide currently! Thanks for the info!
Copy link to clipboard
Copied
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);
};
Copy link to clipboard
Copied
Great thanks! That is very similar to what I have BUT this
exportOptions = new ExportOptionsSaveForWeb();
makes the difference. I will try it out! Thanks for all the help!
Copy link to clipboard
Copied
I would think all you would need to do is download and install Image process pro Photoshop Plug-in You could save for web 10 different size png files for each of your source documents in the first action step then save 5 more in the second action step.
Copy link to clipboard
Copied
Thanks for the reply. Solving these sorts of issues is really helpful in my developer training and much more satisfying. Were I a layman I would just grab a script from somewhere. Thank you for the information though
Copy link to clipboard
Copied
Thanks a bunch that solved it! Here is the new code:
#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');
var options = new ExportOptionsSaveForWeb();
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;
//export the image
doc.exportDocument(file, ExportType.SAVEFORWEB, options);
}
Copy link to clipboard
Copied
Glad that worked! Which would appear to point to the issue being metadata, most probably photoshop:DocumentAncestors related.
I would go back and examine all of your source files and remove this metadata using one of the methods in my blogpost.
Copy link to clipboard
Copied
12.5Mb down to 2k That is a crazy amount of meta data...
Find more inspiration, events, and resources on the new Adobe Community
Explore Now