Skip to content

Instantly share code, notes, and snippets.

@STAR-ZERO
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save STAR-ZERO/12f9b2084efb793c8692 to your computer and use it in GitHub Desktop.
Save STAR-ZERO/12f9b2084efb793c8692 to your computer and use it in GitHub Desktop.
iOSで@3xの画像から@1x@2xの画像にリサイズするPhotoshopスクリプト
// フォルダを指定して一括リサイズ
function resizeImage(file, destFolder) {
try {
var doc = open(file, OpenDocumentType.PNG)
if (doc == null) {
throw "Something is wrong with the file. Make sure it's a valid PNG file.";
}
var startState = doc.activeHistoryState;
var initialPrefs = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS
var sfw = new ExportOptionsSaveForWeb();
sfw.format = SaveDocumentType.PNG;
sfw.PNG8 = false; // use PNG-24
sfw.transparency = true;
doc.info = null; // delete metadata
var baseWidth = Math.floor(doc.width / 3.0);
var baseHeight = Math.floor(doc.height / 3.0);
var filename = doc.name;
filename = filename.replace(".png", "");
filename = filename.replace("@3x", "");
var images = [
{"ext": "", "width": baseWidth, "height": baseHeight},
{"ext": "@2x", "width": baseWidth * 2, "height": baseHeight * 2},
{"ext": "@3x", "width": baseWidth * 3, "height": baseHeight * 3}
];
for (var i = 0; i < images.length; i++) {
var image = images[i];
doc.resizeImage(image.width, image.height, null, ResampleMethod.BICUBICSHARPER);
var outPath = destFolder + "/" + filename + image.ext + ".png";
doc.exportDocument(new File(outPath), ExportType.SAVEFORWEB, sfw);
doc.activeHistoryState = startState
}
return true;
} catch (exception) {
if ((exception != null) && (exception != "")) {
alert(exception);
}
return false;
} finally {
if (doc != null) {
doc.close(SaveOptions.DONOTSAVECHANGES);
}
app.preferences.rulerUnits = initialPrefs; // restore prefs
}
}
var inFolder = Folder.selectDialog("Choose a input folder");
var destFolder = Folder.selectDialog("Choose an output folder");
var fileList = inFolder.getFiles("*.png");
var result = true;
for (var i = 0; i < fileList.length; i++) {
if (!resizeImage(fileList[i], destFolder)) {
result = false;
break;
}
}
if (result) {
alert("Resized!");
}
// ファイルを1つずつ選択
try {
var at3ximage = File.openDialog("Select @3x file", "*.png", false);
if (at3ximage == null) {
throw "";
}
var doc = open(at3ximage, OpenDocumentType.PNG)
if (doc == null) {
throw "Something is wrong with the file. Make sure it's a valid PNG file.";
}
var startState = doc.activeHistoryState;
var initialPrefs = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS
var destFolder = Folder.selectDialog("Choose an output folder");
if (destFolder == null) {
throw "";
}
var sfw = new ExportOptionsSaveForWeb();
sfw.format = SaveDocumentType.PNG;
sfw.PNG8 = false; // use PNG-24
sfw.transparency = true;
doc.info = null; // delete metadata
var baseWidth = Math.floor(doc.width / 3.0);
var baseHeight = Math.floor(doc.height / 3.0);
var filename = doc.name;
filename = filename.replace(".png", "");
filename = filename.replace("@3x", "");
var images = [
{"ext": "", "width": baseWidth, "height": baseHeight},
{"ext": "@2x", "width": baseWidth * 2, "height": baseHeight * 2},
{"ext": "@3x", "width": baseWidth * 3, "height": baseHeight * 3}
];
for (var i = 0; i < images.length; i++) {
var image = images[i];
doc.resizeImage(image.width, image.height, null, ResampleMethod.BICUBICSHARPER);
var outPath = destFolder + "/" + filename + image.ext + ".png";
doc.exportDocument(new File(outPath), ExportType.SAVEFORWEB, sfw);
doc.activeHistoryState = startState
}
alert("Resized!")
} catch (exception) {
if ((exception != null) && (exception != "")) {
alert(exception);
}
} finally {
if (doc != null) {
doc.close(SaveOptions.DONOTSAVECHANGES);
}
app.preferences.rulerUnits = initialPrefs; // restore prefs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment