resize images in upload-to-s3

This commit is contained in:
Sami Samhuri 2014-12-21 15:41:19 -08:00
parent 52d970b4d6
commit 486064b79e

View file

@ -1,5 +1,36 @@
#!/bin/zsh #!/bin/zsh
filename=$(basename "$1") set -e # bail on errors
s3cmd put "$1" s3://static.samhuri.net/"$filename"
function filesize() {
stat "$1" | cut -d' ' -f8
}
touch ~/wtf
big_size=$((512 * 1024))
max_size=$((2 * 1024 * 1024))
img_path="$1"
ls -lh "$img_path"
if [[ $(filesize "$img_path") -gt $big_size ]]; then
echo "BIG, HALVING"
convert -resize "50%" "$img_path" "$img_path"
ls -lh "$img_path"
if [[ $(filesize "$img_path") -gt $big_size ]]; then
echo "STILL BIG, CLAMPING TO 2560x1440"
convert -resize "2560x1440>" "$img_path" "$img_path"
ls -lh "$img_path"
if [[ $(filesize "$img_path") -gt $max_size ]]; then
echo "HUGE, CONVERTING TO JPEG (75% QUALITY)"
new_img_path="${img_path%.png}.jpg"
convert -quality 75 "$img_path" "$new_img_path"
img_path="$new_img_path"
ls -lh "$img_path"
fi
fi
fi
filename=$(basename "$img_path")
# s3cmd put "$img_path" s3://static.samhuri.net/"$filename"
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 ruby -rerb -e "print 'http://static.samhuri.net/' + ERB::Util.url_encode('$filename')" | pbcopy