Skip to content

Instantly share code, notes, and snippets.

@Quinten
Created October 1, 2015 11:51
Show Gist options
  • Save Quinten/f2dc6b463c876f98e0d5 to your computer and use it in GitHub Desktop.
Save Quinten/f2dc6b463c876f98e0d5 to your computer and use it in GitHub Desktop.
Javascript resize script for photoshop. Put it in /Applications/Photoshop/Presets/Scripts . Then run from File → Scripts → My Resize Script
// resize min 900 x 500
// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);
// these are our values for the end result width and height (in pixels) of our image
var fWidth = 900;
var fHeight = 500;
var resolution = 300;
var jpegQuality = 12;
var newRatio = fWidth / fHeight;
var origRatio = doc.width / doc.height;
// do the resizing.
if (origRatio > newRatio) {
if (doc.height < fHeight ) {
// blow up
doc.resizeImage(null,UnitValue(fHeight,"px"),resolution,ResampleMethod.BICUBIC);
} else if (doc.height > (4 * fHeight)) {
// scale down
doc.resizeImage(null,UnitValue((4 * fHeight),"px"),resolution,ResampleMethod.BICUBIC);
} else {
// only change resolution
doc.resizeImage(null,UnitValue(doc.height,"px"),resolution,ResampleMethod.BICUBIC);
}
} else {
if (doc.width < fWidth ) {
// blow up
doc.resizeImage(UnitValue(fWidth,"px"),null,resolution,ResampleMethod.BICUBIC);
} else if (doc.width > (4 * fWidth)) {
// scale down
doc.resizeImage(UnitValue((4 * fWidth),"px"),null,resolution,ResampleMethod.BICUBIC);
} else {
// only change resolution
doc.resizeImage(UnitValue(doc.width,"px"),null,resolution,ResampleMethod.BICUBIC);
}
}
//alert(doc.path);
var newFolder = '~/Documents/Resizing/test/final';
// our web export options
//var options = new ExportOptionsSaveForWeb();
//options.quality = 100;
//options.format = SaveDocumentType.JPEG;
//options.optimized = true;
//var newName = doc.name+'.jpg';
//doc.exportDocument(File(newFolder+'/'+newName),ExportType.SAVEFORWEB,options);
var newName = doc.name.replace(/\.[^\.]+$/, '');
var saveFile = File(newFolder + "/" + newName + ".jpg");
var jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
// close the original document without saving
doc.close(SaveOptions.DONOTSAVECHANGES);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment