Skip to content

Instantly share code, notes, and snippets.

@mtrung
Last active April 1, 2017 01:08
Show Gist options
  • Save mtrung/e0f4a17b24e202e3af8356c88e062c0d to your computer and use it in GitHub Desktop.
Save mtrung/e0f4a17b24e202e3af8356c88e062c0d to your computer and use it in GitHub Desktop.
// Photoshop script to resize canvas and export
// You can add this script to Action for automation
doc = app.activeDocument;
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);
// default background color filling in when resizing canvas
var bgColor = new SolidColor();
bgColor.rgb.hexValue = "000000";
app.backgroundColor = bgColor;
// our web export options
var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.PNG;
options.optimized = true; // default
options.includeProfile = false;
options.transparency = false;
options.PNG8 = true; // default
options.colors = 128;
options.colorReduction = ColorReductionType.SELECTIVE; // default
options.dither = Dither.DIFFUSION; // default
options.ditherAmount = 88; //amount of dither. Only valid for diffusion ( default: 100 )
function exportResizedImage(fWidth, fHeight, outputFolderStr) {
// these are our values for the END RESULT width and height (in pixels) of our image
// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
} else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}
// Convert the canvas size as informed above for the END RESULT
app.activeDocument.resizeCanvas(UnitValue(fWidth,"px"),UnitValue(fHeight,"px"));
var array = doc.path.toString().split("/");
var currFolder = array[array.length-1];
var newName = doc.path + outputFolderStr + currFolder;
var outputFolder = Folder(newName);
if (!outputFolder.exists) outputFolder.create();
newName += "/"+doc.name;
doc.exportDocument(File(newName), ExportType.SAVEFORWEB, options);
}
// Change outputFolderStr to yours
exportResizedImage(454, 454, "/../");
exportResizedImage(390, 390, "/../");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment