Skip to content

Instantly share code, notes, and snippets.

@donnywals
Last active April 28, 2022 18:30
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save donnywals/3be3774435cf53133ee7f8c553e9c080 to your computer and use it in GitHub Desktop.
Save donnywals/3be3774435cf53133ee7f8c553e9c080 to your computer and use it in GitHub Desktop.
#!/bin/sh
# make sure you have imagemagick installed: brew install imagemagick
# your app_icons.sh file should have the correct permissions: run `chmod 775 app_icons.sh` in your terminal from where you put this file
# put your `my_icon.png` next to this file and run ./app_icons.sh to export your app icons
x=my_icon.png
y=${x%.*}
# delete the export directory so we start clean
rm -r converted/$y
# create a fresh export firectory
mkdir converted/$y
# export all images
convert $x -resize 20x20 converted/$y/$y-20x20@1x.png
convert $x -resize 40x40 converted/$y/$y-20x20@2x.png
convert $x -resize 60x60 converted/$y/$y-20x20@3x.png
convert $x -resize 29x29 converted/$y/$y-29x29@1x.png
convert $x -resize 58x58 converted/$y/$y-29x29@2x.png
convert $x -resize 87x87 converted/$y/$y-29x29@3x.png
convert $x -resize 40x40 converted/$y/$y-40x40@1x.png
convert $x -resize 80x80 converted/$y/$y-40x40@2x.png
convert $x -resize 120x120 converted/$y/$y-40x40@3x.png
convert $x -resize 60x60 converted/$y/$y-60x60@1x.png
convert $x -resize 120x120 converted/$y/$y-60x60@2x.png
convert $x -resize 180x180 converted/$y/$y-60x60@3x.png
convert $x -resize 76x76 converted/$y/$y-76x76@1x.png
convert $x -resize 152x152 converted/$y/$y-76x76@2x.png
convert $x -resize 228x228 converted/$y/$y-76x76@3x.png
convert $x -resize 167x167 converted/$y/$y-83_5x83_5@2x.png
# copy input file as the 1024x1024 icon
cp $x converted/$y/$y-1024x1024@1x.png
@isadon
Copy link

isadon commented Dec 10, 2020

#!/bin/sh

# make sure you have imagemagick installed: brew install imagemagick
# your app_icons.sh file should have the correct permissions: run `chmod 755 app_icons.sh` in your terminal from where you put this file
# run ./app_icons.sh <png_file> to export your app icons. Image file designated by <png_file> should be 1024x1024.

file=$1

if [ -z "$file" ]
then
  echo "<png_file> argument not specified."
  exit 1
elif [ ! -f "$file" ]
then
  echo "$file was not found."
  exit 1
fi

file_no_ext=${file%.*}

# delete the export directory so we start clean
rm -r "converted/$file_no_ext"

# create a fresh export firectory
mkdir "converted/$file_no_ext"

# export all images
sq_pt_targets="20 29 40 60 76"
pt_scales="1 2 3"

for sq_pt_target in $sq_pt_targets
do
  for pt_scale in $pt_scales
  do
    px_target="$(bc <<< "$sq_pt_target * $pt_scale")"
    sq_px_target="${px_target}x${px_target}"
    # echo "px_target is: $px_target, sq_px_target is: $sq_px_target"
    convert $file -resize $sq_px_target "converted/$file_no_ext/$file_no_ext-$sq_pt_target@${pt_scale}x.png"
  done
done

# export the single scale images
convert $file -resize 167x167 converted/$file_no_ext/$file_no_ext-83_5x83_5@2x.png
cp $file converted/$file_no_ext/$file_no_ext-1024x1024@1x.png

Attached is the same but with a for loop and allows the script to take the filename as an argument instead of it expecting it to be my_icon.png

I tried quickly getting it to work with svg/pdf's but ImageMagick has issues doing this conversion in an easy way for us since it wants the density parameter. inkscape seems to do a good job but would require installation as I don't believe its normally bundled in the system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment