add deretina and jpegify, improve upload-to-s3

This commit is contained in:
Sami Samhuri 2015-03-24 15:40:49 -07:00
parent b9345e3403
commit 325b692e49
3 changed files with 40 additions and 23 deletions

7
deretina Executable file
View file

@ -0,0 +1,7 @@
#!/bin/zsh
set -e # bail on errors
TWOX_IMG_PATH="$1"
ONEX_IMG_PATH="${TWOX_IMG_PATH%.png}@1x.png"
convert -resize "50%" "$TWOX_IMG_PATH" "$ONEX_IMG_PATH"

8
jpegify Executable file
View file

@ -0,0 +1,8 @@
#!/bin/zsh
set -e # bail on errors
IMG_PATH="$1"
QUALITY="${2:-95}"
JPEG_IMG_PATH="${IMG_PATH%.*}.jpg"
convert -quality $QUALITY "$IMG_PATH" "$JPEG_IMG_PATH"

View file

@ -6,29 +6,31 @@ function filesize() {
stat "$1" | cut -d' ' -f8 stat "$1" | cut -d' ' -f8
} }
big_size=$((512 * 1024)) ORIG_IMG_PATH="$1"
max_size=$((2 * 1024 * 1024))
img_path="$1" # ignore screenshots created by this script or upload-to-s3
ls -lh "$img_path" if [[ "$ORIG_IMG_PATH" == *"@1x.png" ]] || [[ "$ORIG_IMG_PATH" == *"@1x.jpg" ]]; then
if [[ $(filesize "$img_path") -gt $big_size ]]; then exit 0
echo "BIG, HALVING" fi
convert -resize "50%" "$img_path" "$img_path"
ls -lh "$img_path" BIG_SIZE=$((666 * 1024))
if [[ $(filesize "$img_path") -gt $big_size ]]; then
echo "STILL BIG, CLAMPING TO 2560x1440" WEB_IMG_PATH="$ORIG_IMG_PATH"
convert -resize "2560x1440>" "$img_path" "$img_path" ls -lh "$WEB_IMG_PATH"
ls -lh "$img_path" if [[ $(filesize "$ORIG_IMG_PATH") -gt $BIG_SIZE ]]; then
if [[ $(filesize "$img_path") -gt $max_size ]]; then echo "TOO BIG, DERETINA..."
echo "HUGE, CONVERTING TO JPEG (75% QUALITY)" WEB_IMG_PATH="${ORIG_IMG_PATH%.png}@1x.png"
new_img_path="${img_path%.png}.jpg" convert -resize "50%" "$ORIG_IMG_PATH" "$WEB_IMG_PATH"
convert -quality 75 "$img_path" "$new_img_path" ls -lh "$WEB_IMG_PATH"
img_path="$new_img_path" if [[ $(filesize "$WEB_IMG_PATH") -gt $BIG_SIZE ]]; then
ls -lh "$img_path" echo "STILL TOO BIG, COVERTING TO JPEG"
fi WEB_IMG_PATH="${ORIG_IMG_PATH%.png}@1x.jpg"
convert -quality 95 -resize "50%" "$ORIG_IMG_PATH" "$WEB_IMG_PATH"
ls -lh "$WEB_IMG_PATH"
fi fi
fi fi
filename=$(basename "$img_path") FILENAME=$(basename "$WEB_IMG_PATH")
s3cmd put "$img_path" s3://static.samhuri.net/"$filename" FILENAME="${FILENAME// /_}"
ruby -rerb -e "puts 'http://static.samhuri.net/' + ERB::Util.url_encode('$filename')" s3cmd put "$WEB_IMG_PATH" s3://static.samhuri.net/"$FILENAME"
ruby -rerb -e "print 'http://static.samhuri.net/' + ERB::Util.url_encode('$filename')" | pbcopy ruby -rerb -e "puts 'http://static.samhuri.net/' + ERB::Util.url_encode('$FILENAME')"
ruby -rerb -e "print 'http://static.samhuri.net/' + ERB::Util.url_encode('$FILENAME')" | pbcopy