Skip to content

Instantly share code, notes, and snippets.

@shreyasminocha
Created December 25, 2018 15:51
Show Gist options
  • Save shreyasminocha/b49505fce2efa071edd74bd74d6a7105 to your computer and use it in GitHub Desktop.
Save shreyasminocha/b49505fce2efa071edd74bd74d6a7105 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Formatting helpers
RED=$'\e[31m'
GREEN=$'\e[32m'
NORMAL=$'\e[0m'
redEcho() { echo "$RED$1$NORMAL"; }
greenEcho() { echo "$GREEN$1$NORMAL"; }
# Main
if [ ! -d static/images/project-logos ]; then
redEcho "Please run this script from the root of your 'mediawiki-config' clone."
exit 1
fi
for file in "$@"; do
filename=$(basename "$file")
projectName="${filename%.*}"
if [ ! -r "static/images/project-logos/$projectName.png" ]; then
redEcho "$projectName doesn't have a 1x png in static/images/project-logos. Skipping."
continue
fi
oneAndHalfOut="static/images/project-logos/$projectName-1.5x.png"
doubleOut="static/images/project-logos/$projectName-2x.png"
if [ -e "$oneAndHalfOut" ] || [ -e "$doubleOut" ]; then
redEcho "$filename already has -1.5x and/or -2x logos in static/images/project-logos. Skipping."
continue
fi
extension="${filename##*.}"
if [ ! "$extension" = 'svg' ]; then
echo "$filename is an svg file. Skipping."
continue
fi
originalSize=$(file static/images/project-logos/$projectName.png | cut -d ':' -f 2 | cut -d ',' -f 2 | tr -d ' ')
originalWidth=$(echo "$originalSize" | cut -d 'x' -f 1)
originalHeight=$(echo "$originalSize" | cut -d 'x' -f 2)
# 1.5×
oneAndHalfWidth=$(( (originalWidth * 3 ) / 2 ))
oneAndHalfHeight=$(( (originalHeight * 3 ) / 2 ))
convert -resize "${oneAndHalfWidth}x$oneAndHalfHeight" "$file" "$oneAndHalfOut"
if [ "$?" -eq 0 ]; then
greenEcho "Created $(basename $oneAndHalfOut) (${oneAndHalfWidth}×$oneAndHalfHeight)"
else redEcho "Some error occurred :("; fi
# 2×
doubleWidth=$(( originalWidth * 2 ))
doubleHeight=$(( originalHeight * 2 ))
convert -resize "${doubleWidth}x$doubleHeight" "$file" "$doubleOut"
if [ "$?" -eq 0 ]; then
greenEcho "Created $(basename $doubleOut) (${doubleWidth}×$doubleHeight)"
else redEcho "Some error occurred :("; fi
# Optimisation
optipng -o7 "$oneAndHalfOut" "$doubleOut"
if [ "$?" -eq 0 ]; then
greenEcho "Optimised $(basename $oneAndHalfOut) and $(basename $doubleOut)"
else redEcho "Some error occurred :("; fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment