Permalink
Browse files

Rewrite resource optimization script to:

- Use make, so optimization only happens for new resources (remove files to regenerate)
- Automatically make files that are under a certain limit (400,000 pixels) high quality JP2s (for Apple Watch)
  • Loading branch information...
eelco committed Nov 22, 2016
1 parent 9d8b135 commit 26d951142023722914f9eeac03ee71ebc594d831
Showing with 79 additions and 27 deletions.
  1. +1 −1 Makefile
  2. +18 −0 scripts/jp2-convert.sh
  3. +0 −26 scripts/resources-optimize.py
  4. +60 −0 scripts/resources-optimize.sh
View
@@ -73,4 +73,4 @@ site-upload: bootstrap site-build
# Resources
resources:
python scripts/resources-optimize.py
scripts/resources-optimize.sh
View
@@ -0,0 +1,18 @@
#!/bin/bash
INPUT=$1
OUTPUT=$2
STANDARD_QUALITY=$3
SMALL_PIXEL_LIMIT=$4
SMALL_QUALITY=$5
# ---
XPATH_SCRIPT="number(//key[text()='pixelWidth']/following-sibling::integer[1]) * number(//key[text()='pixelHeight']/following-sibling::integer[1]) < $SMALL_PIXEL_LIMIT"
QUALITY=$STANDARD_QUALITY
if [ -n "`sips -g allxml "$INPUT" | xmllint --xpath "$XPATH_SCRIPT" - | grep true`" ]; then
QUALITY=$SMALL_QUALITY
fi
sips -s format jp2 "$INPUT" -s formatOptions $QUALITY --out "$OUTPUT"

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,60 @@
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
if [ -z "`which optipng`" ]; then
brew install optipng
fi
if [ -z "`which cwebp`" ]; then
brew install webp
fi
SCRIPT_PATH="$( cd "$( dirname "$0" )" && pwd )"
cd "extras"
SOURCE_PATH=DeviceResources
TARGET_PATH=resources.framerjs.com/static/DeviceResources
JP2_QUALITY=25
JP2_QUALITY_SMALL=100
WEBP_QUALITY=90
SMALL_PIXEL_LIMIT=400000
mkdir -p "$TARGET_PATH"
cd "$SOURCE_PATH"
pax -wrs'/png$/unoptim.png/' *.png "../$TARGET_PATH"
cd - > /dev/null
cd "$TARGET_PATH"
cat > "Makefile" <<EOF
UNOPTIMIZED_FILES := \$(wildcard *.unoptim.png)
PNG_FILES := \$(patsubst %.unoptim.png, %.png, \$(UNOPTIMIZED_FILES))
JP2_FILES := \$(patsubst %.unoptim.png, %.jp2, \$(UNOPTIMIZED_FILES))
WEBP_FILES := \$(patsubst %.unoptim.png, %.webp, \$(UNOPTIMIZED_FILES))
all: png jp2 webp
png: \$(PNG_FILES)
jp2: \$(JP2_FILES)
webp: \$(WEBP_FILES)
\$(PNG_FILES): %.png: %.unoptim.png
optipng -clobber \$< -out \$@
\$(JP2_FILES): %.jp2: %.unoptim.png
$SCRIPT_PATH/jp2-convert.sh \$< \$@ $JP2_QUALITY $SMALL_PIXEL_LIMIT $JP2_QUALITY_SMALL
\$(WEBP_FILES): %.webp: %.unoptim.png
cwebp -q $WEBP_QUALITY \$< -o \$@
EOF
make all
rm *.unoptim.png
rm "Makefile"

0 comments on commit 26d9511

Please sign in to comment.