Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #target photoshop
- app.bringToFront();
- var Constants = {
- Author: "Antelle",
- OutputImageQuality: 11,
- FileMask: "*.*g",
- InputDir: "d:\\Photos4Habr",
- OutputDir: "d:\\Photos4Habr\\Output",
- AddLogo: {
- LogoPath: "d:\\Logo.psd"
- },
- Resize: {
- MaxWidth: 400,
- MaxHeight: 600
- }
- };
- ProcessDir(Constants.InputDir, Constants.OutputDir);
- function ProcessDir(dir, outDir) {
- var folder = Folder(dir);
- var files = folder.getFiles(Constants.FileMask);
- var outFolder = Folder(outDir);
- if (!outFolder.exists) {
- if (!outFolder.create()) {
- alert("Cannot create output folder");
- return;
- }
- }
- var totalFiles = 0;
- for (var fileNum in files) {
- var outFile = GetOutputFileName(files[fileNum], outFolder.fullName);
- AddFrameToFile(files[fileNum], outFile);
- totalFiles++;
- }
- alert(totalFiles + " files processed");
- }
- function GetOutputFileName(file, outDir) {
- return outDir + /(\/[^\/]+$)/.exec(file)[1];
- }
- function SaveFile(outputFile) {
- var isPng = /png$/i.test(outputFile);
- var saveOptions;
- if (isPng) {
- saveOptions = new PNGSaveOptions();
- saveOptions.interlaced = false;
- } else {
- saveOptions = new JPEGSaveOptions();
- saveOptions.embedColorProfile = true;
- saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
- saveOptions.matte = MatteType.NONE
- saveOptions.quality = Constants.OutputImageQuality;
- }
- app.activeDocument.saveAs(File(outputFile), saveOptions, true, Extension.LOWERCASE)
- app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
- }
- function AddLogoToFile(file, outputFile) {
- var photoFile = File(file);
- var logoFile = File(Constants.AddLogo.LogoPath);
- app.open(logoFile);
- app.activeDocument.artLayers["Text"].copy();
- var logoWidth = app.activeDocument.width.as("px");
- var logoHeight = app.activeDocument.height.as("px");
- app.activeDocument.close();
- app.open(photoFile);
- var width = app.activeDocument.width.as("px");
- var height = app.activeDocument.height.as("px");
- var logoLayer = app.activeDocument.artLayers.add();
- logoLayer.name = "Logo";
- app.activeDocumetn
- app.activeDocument.paste();
- var shape = [
- [(width - logoWidth) / 2, (height - logoHeight) / 2],
- [(width - logoWidth) / 2, (height + logoHeight) / 2],
- [(width + logoWidth) / 2, (height + logoHeight) / 2],
- [(width + logoWidth) / 2, (height - logoHeight) / 2]
- ];
- app.activeDocument.selection.select(shape);
- app.activeDocument.selection.translate(
- new UnitValue((width - logoWidth)/ 2, "px"),
- new UnitValue((height - logoHeight) / 2, "px"));
- var minImageDimension = Math.min(width, height);
- var logoScaleMultiplier = minImageDimension / 5 / logoWidth * 100;
- app.activeDocument.selection.resize(logoScaleMultiplier, logoScaleMultiplier, AnchorPosition.BOTTOMRIGHT);
- app.activeDocument.selection.deselect();
- app.activeDocument.artLayers["Logo"].opacity = 75;
- app.activeDocument.artLayers["Logo"].blendMode = BlendMode.LUMINOSITY;
- SaveFile(outputFile);
- }
- function AddFrameToFile(file, outputFile) {
- var photoFile = File(file);
- app.open(photoFile);
- var width = app.activeDocument.width.as("px");
- var height = app.activeDocument.height.as("px");
- var backgroungLayer = app.activeDocument.artLayers[0];
- backgroungLayer.isBackgroundLayer = false;
- var frameLayer = app.activeDocument.artLayers.add();
- frameLayer.name = "Frame";
- frameLayer.move(backgroungLayer, ElementPlacement.PLACEAFTER);
- app.activeDocument.resizeCanvas(new UnitValue(width + 10, "px"), new UnitValue(height + 10, "px"), AnchorPosition.MIDDLECENTER);
- var color = new SolidColor();
- color.rgb.red = 0xff;
- color.rgb.green = 0xff;
- color.rgb.blue = 0xff;
- app.activeDocument.selection.fill(color);
- var shape = [
- [3, 3],
- [3, height + 7],
- [width + 7, height + 7],
- [width + 7, 3]
- ];
- app.activeDocument.selection.select(shape, SelectionType.REPLACE, 1, false);
- color = new SolidColor();
- color.rgb.red = 0xa9;
- color.rgb.green = 0xcc;
- color.rgb.blue = 0xf5;
- app.activeDocument.selection.fill(color);
- SaveFile(outputFile);
- }
- function ResizeFile(file, outputFile) {
- var photoFile = File(file);
- app.open(photoFile);
- var width = app.activeDocument.width.as("px");
- var height = app.activeDocument.height.as("px");
- var widthScaleFactor = width / Constants.Resize.MaxWidth;
- var heightScaleFactor = height / Constants.Resize.MaxHeight;
- var maxScaleFactor = Math.max(widthScaleFactor, heightScaleFactor);
- if (maxScaleFactor > 1) {
- app.activeDocument.resizeImage(
- new UnitValue(width / maxScaleFactor, "px"),
- new UnitValue(height / maxScaleFactor, "px"));
- }
- SaveFile(outputFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment