Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Process all of those images before you upload them to your site. Here is a photoshop script to do that. File -> Scripts -> Browse
// A Photoshop script to process a folder of images. It creates 3 versions of each image
// e.g. some-image-name.jpg will produce
// * some-image-name-full.jpg
// * some-image-name-mobile.jpg
// * some-image-name-thumbnail.jpg
var IMAGE_QUALITY = 5;
var VALID_IMAGE_TYPES = Array( "jpg", "png", "gif");
main();
function main(){
// Prompts user to select folder
var startFolderPath = "~"
var inputFolder = Folder(startFolderPath).selectDlg("Select a folder to process");
// Get's all jpg files in the selected folder
if (inputFolder == null){
//if nothing selected, exit script
return;
}
var fileList = inputFolder.getFiles();
processImages(fileList);
}
function processImages(fileList){
// Iterate through files in the selected folder
for(var i=0; i<fileList.length; i++) {
var file = fileList[i];
if (isValidFile(file)) {
open(fileList[i]);
var doc = app.activeDocument
var fileName = doc.name.replace(/\.[^\.]+$/, '');
// Save main image at correct quality
saveImage(doc, fileName + '-full');
// Resize and crop for mobile
var height = 264; var width = 505; var top = 20;
doc.resizeImage(width);
doc.crop(new Array(0, top, doc.width,height + top), 0, width, height)
saveImage(doc, fileName + '-mobile');
// Resize for thumbnail
var thumbW = 163; var thumbH = 78;
doc.resizeImage(thumbW, thumbH);
saveImage(doc, fileName + '-thumbnail');
// all done!
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
// Checks to make sure it is actually a file, that it isn't hidden, and that it is a valid file type
function isValidFile(file){
if(file instanceof File && !file.hidden && isValidFileType(file.toString())){
return true;
}
return false;
}
function isValidFileType(fileName){
// make sure it has a file extension
var lastDot = fileName.toString().lastIndexOf( "." );
if ( lastDot == -1 ) {
return false;
}
// check file types
var extension = fileName.substr( lastDot + 1, fileName.length - lastDot ).toLowerCase();
for (var i = 0; i < VALID_IMAGE_TYPES.length; i++ ) {
if ( extension == VALID_IMAGE_TYPES[i] ) {
return true;
}
}
return false;
}
function saveImage(doc, fileName){
var saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = IMAGE_QUALITY;
var saveFile = new File(decodeURI(app.activeDocument.path) + '/' + fileName + '.jpg');
doc.saveAs( saveFile, saveOptions, true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment