Add script to convert png to icns on osx

This commit is contained in:
Jia Hao 2016-01-27 10:22:22 +08:00
parent f4130f940f
commit dbd660b78f
6 changed files with 185 additions and 0 deletions

103
bin/pngToIcns.sh Executable file
View File

@ -0,0 +1,103 @@
#!/bin/sh
# modified from https://github.com/daveish/png2icns for OSX 10.11
# USAGE
# ./pngToIcns <input png> <outp icns>
# Example
# ./pngToIcns.sh ~/sample.png ~/Desktop/converted.icns
# Exec Paths
SIPS=$(which sips)
ICONUTIL=$(which iconutil)
IMAGEMAGICK=$(which convert)
if [ ! -x "${SIPS}" ]; then
echo "Cannot find required sips executable" >&2
exit 1;
fi
if [ ! -x "${ICONUTIL}" ]; then
echo "Cannot find required iconutil executable" >&2
exit 1;
fi
if [ ! -x "${IMAGEMAGICK}" ]; then
echo "Cannot find required ImageMagick Convert executable" >&2
exit 1;
fi
# Parameters
SOURCE=$1
echo $1
echo $2
# Get source image
if [ -z "${SOURCE}" ]; then
echo "No source image specified"
exit 1
fi
# File Infrastructure
NAME=$(basename "${SOURCE}")
EXT="${NAME##*.}"
BASE="${NAME%.*}"
ICONSET="${BASE}.iconset"
# Debug Info
# echo "SOURCE: ${SOURCE}"
# echo "NAME: $NAME"
# echo "BASE: $BASE"
# echo "EXT: $EXT"
# echo "ICONSET: $ICONSET"
# Get source image info
SRCWIDTH=$( sips -g pixelWidth "${SOURCE}" | tail -n1 | awk '{print $2}')
SRCHEIGHT=$( sips -g pixelHeight "${SOURCE}" | tail -n1 | awk '{print $2}' )
SRCFORMAT=$( sips -g format "${SOURCE}" | tail -n1 | awk '{print $2}' )
# Debug Info
# echo "SRCWIDTH: $SRCWIDTH"
# echo "SRCHEIGHT: $SRCHEIGHT"
# echo "SRCFORMAT: $SRCFORMAT"
# Check The Source Image
# if [ "x${SRCWIDTH}" != "x1024" ] || [ "x${SRCHEIGHT}" != "x1024" ]; then
# echo "ERR: Source image should be 1024 x 1024 pixels." >&2
# exit 1;
# fi
if [ "x${SRCFORMAT}" != "xpng" ]; then
echo "ERR: Source image format should be png." >&2
exit 1;
fi
# Resample image into iconset
mkdir "${ICONSET}"
# $SIPS -s format png --resampleWidth 1024 "${SOURCE}" --out "${ICONSET}/icon_512x512@2x.png" > /dev/null 2>&1
# $SIPS -s format png --resampleWidth 512 "${SOURCE}" --out "${ICONSET}/icon_512x512.png" > /dev/null 2>&1
# cp "${ICONSET}/icon_512x512.png" "${ICONSET}/icon_256x256@2x.png"
# $SIPS -s format png --resampleWidth 256 "${SOURCE}" --out "${ICONSET}/icon_256x256.png" > /dev/null 2>&1
# cp "${ICONSET}/icon_256x256.png" "${ICONSET}/icon_128x128@2x.png"
# $SIPS -s format png --resampleWidth 128 "${SOURCE}" --out "${ICONSET}/icon_128x128.png" > /dev/null 2>&1
# $SIPS -s format png --resampleWidth 64 "${SOURCE}" --out "${ICONSET}/icon_32x32@2x.png" > /dev/null 2>&1
# $SIPS -s format png --resampleWidth 32 "${SOURCE}" --out "${ICONSET}/icon_32x32.png" > /dev/null 2>&1
# cp "${ICONSET}/icon_32x32.png" "${ICONSET}/icon_16x16@2x.png"
# $SIPS -s format png --resampleWidth 16 "${SOURCE}" --out "${ICONSET}/icon_16x16.png" > /dev/null 2>&1
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 16x16 "${ICONSET}/icon_16x16.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 32x32 "${ICONSET}/icon_16x16@2x.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 32x32 "${ICONSET}/icon_32x32.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 64x64 "${ICONSET}/icon_32x32@2x.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 128x128 "${ICONSET}/icon_128x128.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 256x256 "${ICONSET}/icon_128x128@2x.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 256x256 "${ICONSET}/icon_256x256.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 512x512 "${ICONSET}/icon_256x256@2x.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 512x512 "${ICONSET}/icon_512x512.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 1024x1024 "${ICONSET}/icon_512x512@2x.png"
# Create an icns file lefrom the iconset
iconutil -c icns "${ICONSET}" -o $2
# Clean up the iconset
rm -rf "${ICONSET}"

View File

@ -44,6 +44,7 @@
"ncp": "^2.0.0",
"request": "^2.67.0",
"sanitize-filename": "^1.5.3",
"shelljs": "^0.5.3",
"source-map-support": "^0.4.0",
"tmp": "0.0.28",
"validator": "^4.5.0"

48
src/getIcon.js Normal file
View File

@ -0,0 +1,48 @@
import shell from 'shelljs';
import path from 'path';
import tmp from 'tmp';
tmp.setGracefulCleanup();
const PNG_TO_ICNS_BIN_PATH = path.join(__dirname, '..', 'bin/pngToIcns.sh');
/**
* @callback pngToIcnsCallback
* @param {{}} error
* @param {string} error.stdOut
* @param {string} error.stdError
* @param {string} icnsDest
*/
/**
*
* @param {string} pngSrc
* @param {string} icnsDest
* @param {pngToIcnsCallback} callback
*/
function pngToIcns(pngSrc, icnsDest, callback) {
shell.exec(`${PNG_TO_ICNS_BIN_PATH} ${pngSrc} ${icnsDest}`, {silent: true}, (exitCode, stdOut, stdError) => {
if (exitCode) {
callback({
stdOut: stdOut,
stdError: stdError
});
return;
}
callback(null, icnsDest);
});
}
/**
* Converts the png to a temporary directory which will be cleaned up on process exit
* @param {string} pngSrc
* @param {pngToIcnsCallback} callback
*/
function pngToIcnsTmp(pngSrc, callback) {
const tempIconDirObj = tmp.dirSync({unsafeCleanup: true});
const tempIconDirPath = tempIconDirObj.name;
pngToIcns(pngSrc, `${tempIconDirPath}/icon.icns`, callback);
}
export default pngToIcnsTmp;

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -0,0 +1,33 @@
// need to subtract 2 from source maps
import 'source-map-support/register';
import tmp from 'tmp';
import chai from 'chai';
import fs from 'fs';
import path from 'path';
import pngToIcns from './../../lib/getIcon';
let assert = chai.assert;
function testConvertPng(pngName, done) {
pngToIcns(path.join(__dirname, '../../', 'test-resources', pngName), (error, icnsPath) => {
if (error) {
done(error);
return;
}
let stat = fs.statSync(icnsPath);
assert.isTrue(stat.isFile(), 'Output icns file should be a path')
done();
});
}
describe('Get Icon Module', function() {
it('Can convert a rgb png to icns', function(done) {
testConvertPng('iconSample.png', done);
});
it('Can convert a grey png to icns', function(done) {
testConvertPng('iconSampleGrey.png', done);
});
});