GuildWiki:Interactive map project/PhotoshopTileMakerScript

OutputDir = './';

//******************************************************************************

// we need a document to work with if (documents.length <= 0) { alert('Please open the orignal map material first.'); } else {   // size of a single tile (set by Google and is fixed) var tileSize = 256; // define some colors we are using bgBlack = new SolidColor; bgBlack.rgb.red = 0; bgBlack.rgb.green = 0; bgBlack.rgb.blue = 0;

// before we do anything, make sure photoshop is using pixel as a measurement unit preferences.rulerUnits = Units.PIXELS;

// calculate how many tiles we are going to need orginalImageMaxSize = Math.max(activeDocument.width, activeDocument.height); tileNum = Math.ceil(orginalImageMaxSize/tileSize); // calculate the new size of the image in square form newImageSize = tileSize*tileNum; // create a copy of the orginal document that we will work with var srcDoc = activeDocument; srcDoc.selection.select(Array(Array(0, 0), Array(srcDoc.width, 0), Array(srcDoc.width, srcDoc.height), Array(0, srcDoc.height)),        SelectionType.REPLACE, 0, false); srcDoc.selection.copy; backgroundColor = bgBlack; var documentWorkingCopy = documents.add(newImageSize, newImageSize, srcDoc.resolution,        "Original Image Working Copy", NewDocumentMode.RGB, DocumentFill.BACKGROUNDCOLOR); documentWorkingCopy.paste;

// modify the size of the document to a square newImageSize = tileSize*tileNum; documentWorkingCopy.resizeCanvas(newImageSize, newImageSize); documentWorkingCopy.flatten; // make sure we keep the black background // calculate how many zoom levels we need/can support maxSupportedLevel = Math.ceil(Math.log(tileNum) / Math.log(2)); // loop through all levels for which we have to create tiles for (var level=4; level<=maxSupportedLevel; level++) {       // copy the contents of the orginal image to a new document activeDocument = documentWorkingCopy; var srcDoc = documentWorkingCopy; srcDoc.selection.select(Array(Array(0, 0), Array(srcDoc.width, 0), Array(srcDoc.width, srcDoc.height), Array(0, srcDoc.height)),          SelectionType.REPLACE, 0, false); srcDoc.selection.copy; var documentZoomLevel = documents.add(newImageSize, newImageSize, srcDoc.resolution,          "Zoom Level Copy "+level, NewDocumentMode.RGB, DocumentFill.WHITE); documentZoomLevel.paste; // resize contents of new document to match the size needed for the current zoom level zoomLevelSize = tileSize * Math.pow(2, level); documentZoomLevel.resizeImage(zoomLevelSize, zoomLevelSize,          documentZoomLevel.resolution, ResampleMethod.BICUBIC); // calculate number of tiles in width/height for this zoom level tileNum = Math.pow(2, level); // split into tiles for (var x=0; x<tileNum; x++) {           for (var y=0; y<tileNum; y++) {               // calculate coordinates of the tile tileLeft = x*tileSize; tileTop = y*tileSize; tileRight = (x+1)*tileSize; tileBottom = (y+1)*tileSize; // copy tile and paste into new document activeDocument = documentZoomLevel; var srcDoc = documentZoomLevel; srcDoc.selection.select(Array(Array(tileLeft, tileTop), Array(tileRight, tileTop), Array(tileRight, tileBottom), Array(tileLeft, tileBottom)),                 SelectionType.REPLACE, 0, false); srcDoc.selection.copy; var tileDocument = documents.add(tileSize, tileSize, srcDoc.resolution,                 "Tile "+level+'-'+x+'-'+y, NewDocumentMode.RGB); tileDocument.paste; // save the tile to a file tileDocument.flatten; saveFile = new File(OutputDir+level+'-'+x+"-"+y+".gif"); gifSaveOptions = new GIFSaveOptions; gifSaveOptions.colors = 64; gifSaveOptions.dither = Dither.NONE; gifSaveOptions.matte = MatteType.NONE; gifSaveOptions.preserveExactColors = 0; gifSaveOptions.transparency = 0; gifSaveOptions.interlaced = 0; tileDocument.saveAs(saveFile, gifSaveOptions, true, Extension.LOWERCASE); // close tile document tileDocument.close(SaveOptions.DONOTSAVECHANGES); tileDocument = null; }       }        // close zoom level document documentZoomLevel.close(SaveOptions.DONOTSAVECHANGES); documentZoomLevel = null; }   // close working copy documentWorkingCopy.close(SaveOptions.DONOTSAVECHANGES); documentWorkingCopy = null; }