Skip to content

Instantly share code, notes, and snippets.

@julienhay
Created February 20, 2014 17:24
Show Gist options
  • Save julienhay/9118898 to your computer and use it in GitHub Desktop.
Save julienhay/9118898 to your computer and use it in GitHub Desktop.
Script Photoshop resize image (Portrait and Landscape auto) 1024x -> And generate miniature
// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;
var fWidthBig = 1024;
doc.resizeImage(UnitValue(fWidthBig,"px"),null,null,ResampleMethod.BICUBIC);
var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true;
doc.exportDocument(File(doc.path+'/'+doc.name),ExportType.SAVEFORWEB,options);
// 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 = 156;
var fHeight = 156;
// get mask position and size
var mask = doc.activeLayer;
bounds = mask.bounds;
var maskTop = bounds[0]
var maskLeft = bounds[1];
var maskWidth = bounds[2] - bounds[0];
var maskHeight = bounds[3] - bounds[1];
// 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);
// calculate how much will be cropped out on left/right
offsetLeft = (doc.width-fWidth)/2;
offsetRight = doc.width-offsetLeft;
// crop - left, top, right, bottom
doc.crop([offsetLeft, 0, offsetRight, doc.height]);
}
else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
// calculate how much will be cropped out on top/bottom
offsetTop = (doc.height - fHeight) / 2;
offsetBottom = doc.height-offsetTop;
// crop - left, top, right, bottom
doc.crop([0, offsetTop, doc.width, offsetBottom]);
}
// call autoContrast and applySharpen on the active layer.
// if we just opened a gif, jpeg, or png, there's only one layer, so it must be the active one
doc.activeLayer.autoContrast();
doc.activeLayer.applySharpen();
// our web export options
var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.JPEG;
options.optimized = true;
var newName = 'min-'+doc.name+'';
doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);
doc.close(SaveOptions.DONOTSAVECHANGES);
@mludz
Copy link

mludz commented Jan 12, 2015

For CS6, the script should be put in this location: Photoshop CS6/Presets/Scripts

Documentation is here: http://www.adobe.com/devnet/photoshop/scripting.html

@aburd
Copy link

aburd commented Sep 28, 2016

Thanks for this!

@Said-Ait-Driss
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment