Created
August 27, 2016 12:40
-
-
Save Chaz6/f13980724b3a6274a1684c8bd1931a26 to your computer and use it in GitHub Desktop.
Create a simple image gallery using bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SAVEIFS=${IFS} | |
IFS=$(echo -en "\n\b") | |
THUMBNAIL_DIR=".thumbnails2" | |
THUMBNAIL_SIZE=170 | |
THUMBNAIL_PREFIX="thumbnail-" | |
override=0 | |
while getopts ":o" opt; do | |
case $opt in | |
o) override=1 | |
;; | |
esac | |
done | |
mkdir -p $THUMBNAIL_DIR | |
echo "<html><head>" > index.html | |
echo "<script src=\"//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js\" type=\"text/javascript\"></script>" >> index.html | |
echo "<script src=\"//cdnjs.cloudflare.com/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js\" type=\"text/javascript\"></script>" >> index.html | |
echo "<script src=\"//cdnjs.cloudflare.com/ajax/libs/tinysort/2.2.2/tinysort.min.js\" type=\"text/javascript\"></script>" >> index.html | |
echo "<script type=\"text/javascript\">" >> index.html | |
echo "\$(function(){" >> index.html | |
echo " tinysort('center>a',{order:'desc',attr:'href'});" >> index.html | |
echo " \$(\"img.lazy\").lazyload();" >> index.html | |
echo "});" >> index.html | |
echo "</script>" >> index.html | |
echo "</head><body><center>" >> index.html | |
for filename in *.jpg *.png *.gif *.jpeg *.JPG *.JPEG | |
do | |
if [ -f "${filename}" ] | |
then | |
echo "${filename}" | |
thumbnail_file="${THUMBNAIL_DIR}/${THUMBNAIL_PREFIX}${filename}" | |
if ! [ -f "$thumbnail_file" ] || [ "$override" == 1 ]; | |
then | |
nice -n 19 gm convert -define jpeg:size=$(($THUMBNAIL_SIZE * 2))x$(($THUMBNAIL_SIZE * 2)) "$filename[0]" -thumbnail ${THUMBNAIL_SIZE}x${THUMBNAIL_SIZE}^ -auto-orient -gravity center -extent ${THUMBNAIL_SIZE}x${THUMBNAIL_SIZE} +profile "*" "$thumbnail_file" | |
fi | |
echo '<a href="'${filename}'"><img class="lazy" data-original="'${thumbnail_file}'" width="'${THUMBNAIL_SIZE}'" height="'${THUMBNAIL_SIZE}'"></a>' >> index.html | |
fi | |
done | |
echo "</center></body></html>" >> index.html | |
IFS=${SAVEIFS} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment