Created
November 29, 2016 09:57
-
-
Save ddesignercoder/3e7d8bdb9baa40768f96234aa7f1d727 to your computer and use it in GitHub Desktop.
Image Optimization for web with same file name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Image optimization script with photoshop | |
// Written by: Bhupender Sharma | |
// Version 1.0.2 | |
// Required Adobe Photoshop CS 2 and above | |
//Enable double clicking on Mac Finder & Windows Explorer | |
#target Photoshop | |
//Bring app to front | |
app.bringToFront() | |
// Backup the current Photoshop preferences | |
var startDisplayDialogs = app.displayDialogs, | |
startRulerUnits = app.preferences.rulerUnits; | |
//Set Photoshop to use pixels and display no dialogs | |
app.displayDialogs = DialogModes.NO; | |
app.preferences.rulerUnits = Units.PIXELS; | |
app.preferences.typeUnits = TypeUnits.PIXELS; | |
// Get the Source folder | |
var inputFolder = Folder.selectDialog("Select the Source folder for Images to be optimized"); | |
// Get the Destination folder | |
if (!!inputFolder) { | |
var outputFolder = Folder.selectDialog("Select a folder to store the generated Optimized images"); | |
} | |
if (!!inputFolder && !!outputFolder) { | |
processImagesInFolder(inputFolder); | |
} | |
function processImagesInFolder(folder) { | |
var fileList = folder.getFiles(); | |
for (var i = 0; i < fileList.length; i++) { | |
var file = fileList[i]; | |
if (file instanceof File && (file.name.match(/\.jpg$/i) || file.name.match(/\.png$/i) || file.name.match(/\.gif$/i)) && file.hidden == false) { // The fileList includes both folders and files so open only files | |
open(file); // Open the file | |
//Get Current File's Name | |
var imgfileName = file.name.split('.') | |
//Set Destination file path | |
var saveFile = new File(outputFolder + "/" + imgfileName[0] + "." + file.name.split('.').pop()); | |
saveFile.remove(); // Remove any exisiting file with same name before saving | |
saveAsOptimizedJPEG(saveFile,60); // Saving the file for web with 60% JPEG optimization | |
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); //Close the File | |
} else if (file instanceof Folder) { | |
processFolder(file); // Traverse subfolders | |
} | |
} | |
} | |
function saveAsOptimizedJPEG(saveFile,jpegQuality) { // Optimize Image for Web | |
var sfwOptions = new ExportOptionsSaveForWeb(); | |
sfwOptions.format = SaveDocumentType.JPEG; | |
sfwOptions.includeProfile = false; | |
sfwOptions.interlaced = 0; | |
sfwOptions.optimized = true; | |
sfwOptions.quality = jpegQuality; | |
app.activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions); | |
} | |
// Reset Photoshop preferences to original vlaues | |
app.displayDialogs = startDisplayDialogs; | |
app.preferences.rulerUnits = startRulerUnits; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment