Last active
August 29, 2015 13:55
-
-
Save PauloMigAlmeida/8759593 to your computer and use it in GitHub Desktop.
Converting Images from Android xhdpi to Iphone 1x And 2x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script's goals | |
# | |
# 1 - Trim image | |
# 2 - Create folder for exported ones | |
# 3 - Convert proportionally | |
# | |
# Script usage | |
# | |
# ConvertImageToIphoneSize file.png 720 (Obs.: I'm using XHDPI value just to exemplify how to use it) | |
# ConvertImageToIphoneSize . 720 (Obs.: it'll take every single png file on that folder) | |
# | |
# References to build it | |
# | |
# http://www.unix.com/shell-programming-scripting/42170-function-returns-string.html | |
# http://stackoverflow.com/a/965072/832748 | |
# http://www.imagemagick.org/Usage/resize/ | |
# http://stackoverflow.com/questions/59838/how-to-check-if-a-directory-exists-in-a-shell-script | |
# http://stackoverflow.com/questions/4665051/check-if-passed-argument-is-file-or-directory-in-bash | |
# http://stackoverflow.com/questions/242538/unix-shell-script-find-out-which-directory-the-script-file-resides | |
# Functions used on this script | |
function ruleOfThree(){ | |
A=$1 | |
B=$2 | |
C=$3 | |
BC=`expr $C \* $B` | |
X=`expr $BC / $A` | |
echo $X | |
} | |
function convertToIphoneImages(){ | |
# Goal - 1 - Trim image | |
PWD=$(pwd) | |
PWD=$(printf %q "$PWD") | |
NAME=${1} | |
if [ ${1:2} = "./" ] ; then | |
NAME=${1:2} | |
fi | |
NAME=$(printf %q "$NAME") | |
FULLPATHFILE=$PWD/$NAME | |
eval convert "$FULLPATHFILE" -trim "$FULLPATHFILE" | |
# Getting parameters to be used on conversion process | |
RESOLUTION=$2 | |
WIDTHORIGINAL=$(eval identify -format "%w" "$FULLPATHFILE" | sed 's,^ *,,; s, *$,,') | |
WIDTHIPHONE1X=$(ruleOfThree $RESOLUTION $WIDTHORIGINAL 320) | |
WIDTHIPHONE2X=$(ruleOfThree $RESOLUTION $WIDTHORIGINAL 640) | |
#Separating Filename from extension | |
FILENAME=$(basename "$FULLPATHFILE") | |
EXTENSION="${FILENAME##*.}" | |
FILENAME="${FILENAME%.*}" | |
# Goal - 2 - Create folder for exported ones | |
if [ ! -d "$PWD/exported" ] ; then | |
mkdir -pv "$(pwd)/exported" | |
fi | |
# Goal - 3 - Convert proportionally | |
eval convert "$FULLPATHFILE" -resize $WIDTHIPHONE1X "$PWD/exported/$FILENAME.$EXTENSION" | |
eval convert "$FULLPATHFILE" -resize $WIDTHIPHONE2X "$PWD/exported/$FILENAME@2x.$EXTENSION" | |
} | |
if [ -d "$1" ]; then | |
for dir in $1/*.png | |
do | |
dir=${dir%*/} | |
echo Processing ${dir##*/} | |
convertToIphoneImages "$dir" $2 | |
done | |
elif [ -f "$1" ]; then | |
echo Processing "$1" | |
convertToIphoneImages "$1" $2 | |
else | |
echo "it's not a valid (path|file)" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment