Skip to content

Commit

Permalink
Rewrite resource optimization script to:
Browse files Browse the repository at this point in the history
- 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 26d9511
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ site-upload: bootstrap site-build
# Resources

resources:
python scripts/resources-optimize.py
scripts/resources-optimize.sh
18 changes: 18 additions & 0 deletions scripts/jp2-convert.sh
Original file line number Diff line number Diff line change
@@ -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"
26 changes: 0 additions & 26 deletions scripts/resources-optimize.py

This file was deleted.

60 changes: 60 additions & 0 deletions scripts/resources-optimize.sh
Original file line number Diff line number Diff line change
@@ -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.