Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scirpt that reads in json file and puts text over image and creates thumbnail
#target photoshop
//#include "json2.js" // jshint ignore:line
//Using this file https://github.com/douglascrockford/JSON-js/blob/master/json2.js
//Had to download and install it
main();
var base_dir = ""
function main() {
var initialPrefs = app.preferences.rulerUnits; // will restore at end
app.preferences.rulerUnits = Units.PIXELS; // use pixels
var thumbnailFile = app.activeDocument.path + "/thumbnail.png"
var thumbnailFileFull = app.activeDocument.path + "/thumbnail_FULL.png"
var text_rules_file = app.activeDocument.path + "/image_text.json"
//Base Dir used by saveTxt
base_dir = app.activeDocument.path
//Get the img_text.json file located in same dir
text_json = getJSONFromFile(base_dir +"/img_text.json")
//Make a copy
var docCopy = app.activeDocument.duplicate();
var sfw = new ExportOptionsSaveForWeb();
sfw.format = SaveDocumentType.PNG;
sfw.PNG8 = false; // use PNG-24
sfw.transparency = true;
var goForIt = confirm("This will make a thumbnail\n" + thumbnailFile + "\n\nWith text defined in the " + text_rules_file + " File");
try {
if (goForIt) {
for(x=0; x< text_json.length; x++){
//alert( text_json[x]["text"] );
drawTextLayer(docCopy,
text_json[x]["text"],
text_json[x]["font"],
text_json[x]["font-size"],
text_json[x]["pos"],
text_json[x]["rotate"],
)
}
//Save the Updated file as png
docCopy.exportDocument(new File(thumbnailFileFull), ExportType.SAVEFORWEB, sfw);
//resize to small Twitter size 360 x
docCopy.resizeImage(UnitValue(360, "px"), null, null, ResampleMethod.BICUBIC);
docCopy.exportDocument(new File(thumbnailFile), ExportType.SAVEFORWEB, sfw);
}
}
catch(exception) {
confirm("ERROR: " + exception)
}
finally{
app.preferences.rulerUnits = initialPrefs; // restore prefs
if(docCopy != null){
docCopy.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
//Function for drawing Text on layer
function drawTextLayer(doc, txt, font, fontSize, pos, rotate){
createTextLayer(txt, font, fontSize, pos, rotate)
magicWand(1,1)
textSelection = activeDocument.selection
textSelection.invert()
textSelection.expand(10)
createLayerBelowCurrent("The Layer Below");
var myColor = new SolidColor();
myColor.rgb.hexValue = "000000"
textSelection.fill(myColor);
//For testing purposes write out all fonts
//writeAllFontsToFile();
}
function createTextLayer(txt, font, fontSize, pos, rotate) {
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var thisLayer = activeDocument.artLayers.add();
thisLayer.kind = LayerKind.TEXT;
thisLayer.name = txt;
var textProperty = thisLayer.textItem;
textProperty.kind = TextType.POINTTEXT; //POINTTEXT does not wrap but PARAGRAPHTEXT does
//Font Size
textProperty.size = fontSize;
//textProperty.font = "Orbitron Black";
textProperty.font = font;
var newColor = new SolidColor();
//Font Colour
newColor.rgb.hexValue = "ffffff"
textProperty.color = newColor;
thisLayer.blendMode = BlendMode.NORMAL;
thisLayer.opacity = 100;
textProperty.contents = txt; //In text user /r for carriage return /n does not work
textProperty.position = pos;
app.preferences.rulerUnits=startRulerUnits;
thisLayer.rotate(rotate)
}
function saveTxt(fileName, txt)
{
var saveFile = File(base_dir + "/" + fileName);
if(saveFile.exists)
saveFile.remove();
saveFile.encoding = "UTF8";
saveFile.open("e", "TEXT", "????");
saveFile.writeln(txt);
saveFile.close();
}
//For testing purposes write all font names and
//All postscript font names to file
//postscriptName is what is needed to set font for layer
function writeAllFontsToFile(){
all_fonts = ""
for(var x = 0; x < app.fonts.length; x++){
all_fonts += app.fonts[x].name + "::" + app.fonts[x].postScriptName + "\n"
}
saveTxt("fontfile.txt", all_fonts);
}
function magicWand(x,y,t,a,c,s) {
if(arguments.length < 2) return;// make sure have x,y
if(undefined == t) var t = 32;// set defaults of optional arguments
if(undefined == a) var a = true;
if(undefined == c) var c = false;
if(undefined == s) var s = false;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
desc.putReference( charIDToTypeID('null'), ref );
var positionDesc = new ActionDescriptor();
positionDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), x );// in pixels
positionDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), y );
desc.putObject( charIDToTypeID('T '), charIDToTypeID('Pnt '), positionDesc );
desc.putInteger( charIDToTypeID('Tlrn'), t);// tolerance
desc.putBoolean( charIDToTypeID('Mrgd'), s );// sample all layers
if(!c) desc.putBoolean( charIDToTypeID( "Cntg" ), false );// contiguous
desc.putBoolean( charIDToTypeID('AntA'), a );// anti-alias
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
function createLayerBelowCurrent(astring)
{
var currentActivelayer = app.activeDocument.activeLayer;
var idx = getLayerIndex(currentActivelayer);
// Get a reference to the active layer
var layerRef = app.activeDocument.layers[idx];
// Create a new Art Layer, by default at the top of the document
var newLayerRef = app.activeDocument.artLayers.add();
// Move the new layer set to after the previously first layer
newLayerRef.move(layerRef, ElementPlacement.PLACEAFTER);
}
function getLayerIndex(ref)
{
// return the idex of ALL layers
var numOfLayers = app.activeDocument.layers.length;
// work from the top of the stack down!
for (var i = numOfLayers -1; i >= 0; i--)
{
var tempLayer = app.activeDocument.layers[i];
if (tempLayer == ref) return i
if (tempLayer.typename == "LayerSet")
{
var subDoc = app.activeDocument.layers[i];
for (var j = numOfSubLayers -1; j >= 0; j--)
{
var tempSubLayer = subDoc.layers[j]
if (tempSubLayer == ref) return j
}
}
}
}
function getJSONFromFile(jsonFile){
var jsonFile = File(jsonFile)
jsonFile.encoding = 'UTF8'; // set to 'UTF8' or 'UTF-8'
jsonFile.open("r");
var fileContentsString = jsonFile.read();
jsonFile.close();
return eval(fileContentsString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.