#!/bin/sh # Creates simple web galleries using ImageMagick and Apache Virtual Includes # # Originally written and distributed by Dayid Alan # http://code.dayid.org/asimplegal # # Published under the Pancake Public License v 1.0: http://code.dayid.org/ppl/ppl.txt # # Define important variables: # - Lines including user-adjustable variables are preceeded by ##. # ## URL for the gallery (do not include http:// or a trailing /) GALURL='www.dayid.org' # # Relative directory of your document root. # "public_html" by default, sometimes "html" or "web" pending machine setup ## Should have a trailing / RELDIR='public_html/' # # Remove spaces in filenames: for FILE in *; do NEW=`echo ${FILE} | tr ' ' '_'` [ "${NEW}" != "${FILE}" ] && mv ${FILE} ${NEW} && echo "Renamed: ${FILE} to ${NEW}" done # ## Inserted to your HTML files before the thumbnails/links: TOP="
" BOTTOM="
" PAGE="${TOP}" # # Create the header for index file and echo it into index.html: echo "
" > index.html # # Start the breadcrumb/header/directory links echo -n "${GALURL} / " > .GALBREAD # # Find out where we are, and make breadcrumb links: CURDIR=`pwd | awk -F "${RELDIR}" '{ print $2 }'` DIRDIR=`echo ${CURDIR} | cut -d "/" -f "1"` OLDDIR="" while [ `echo ${CURDIR} | wc -w` -gt '0' ]; do echo -n "${DIRDIR} / " >> .GALBREAD CURDIR=`echo ${CURDIR} | awk -F "${DIRDIR}/" '{ print $2 }'` OLDDIR="${OLDDIR}/${DIRDIR}" DIRDIR=`echo ${CURDIR} | cut -d "/" -f "1"` done # # Add the breakdrumbs to index.html: cat .GALBREAD >> index.html # Add a line before files: echo '
BACK
' >> index.html # # Take note of all files in CURDIR - ignoring hidden: find . -maxdepth "1" -type "f" | sed -e 's/\.\///g' | grep -v ^\\. | grep -v "mediums.html" | grep -v "index.html" > .GAL # # Take note of all directories in CURDIR - ignoring hidden: find . -maxdepth "1" -type "d" | sed -e 's/\.\///g' | grep -v ^\\. | sort -n > .GALDIRS DIRS=`wc -l .GALDIRS | awk '{ print $1 }'` # If there are directories, create a table to contain them and link them if [ ${DIRS} -gt '0' ]; then echo '' >> index.html # For each directory, check for description in .htaccess and use it if possible: for DIR in $( find . -maxdepth "1" -type "d" | sed -e 's/\.\///g' | grep -v ^\\. | sort -n ); do DESCR="" [ -f .htaccess ] && DESCR=`grep \"${DIR}\" .htaccess 2>/dev/null | cut -d '"' -f "2"` echo "" >> index.html grep -v "${DIR}" .GALDIRS > .GALTEMP && mv .GALTEMP .GALDIRS done # Close the table and add another horizontal line echo '
Folder:Description
${DIR}${DESCR}

' >> index.html fi # # Define filetypes to be processed and process them: for EXT in `echo jpg png bmp tif jpeg gif JPG PNG BMP TIF JPEG GIF`; do # Make sure there are files with that extension before trying to process that extension CHECK=$( find . -maxdepth "1" -name "*.${EXT}" -type "f" 2> /dev/null | grep -v '\.m\.'${EXT} | grep -v '\.t\.'${EXT} | wc -l | awk '{ print $1 }' ) if [ ${CHECK} -gt '0' ]; then # Remove that extension from the list of files to be processed grep -v "\.${EXT}" .GAL > .GALTEMP && mv .GALTEMP .GAL for IMG in `find . -maxdepth "1" -name "*.${EXT}" -type "f" | grep -v '\.m\.'${EXT} | grep -v '\.t\.'${EXT} | sort -n`; do echo -n "${IMG}" BASE=`basename ${IMG} .${EXT}` # Create the medium image. if [ -f ${BASE}.m.${EXT} ]; then echo -n " Medium OK" > /dev/null; else echo -n " No medium, creating..." convert ${IMG} -resize ">x600" ${BASE}.m.${EXT} convert ${BASE}.m.${EXT} -resize ">600" ${BASE}.m.${EXT} fi # If the medium image is the same as the fullsize, remove the medium and link the fullsize to it. MATCH="N" # Don't do this check for animated GIFs, since it will hose things up: MEDFILE=`identify ${BASE}.m.${EXT} | awk '{ print $3 }'` FULLFILE=`identify ${BASE}.${EXT} | awk '{ print $3 }'` if [ $MEDFILE = $FULLFILE ]; then echo "Medium matches Full-size, linking image" rm ${BASE}.m.${EXT} && ln -s ${BASE}.${EXT} ${BASE}.m.${EXT} MATCH="Y" fi # Create the thumbnail image. if [ -f ${BASE}.t.${EXT} ]; then echo -n " Thumbnail OK" > /dev/null; else echo -n " No thumbnail, creating..." convert ${BASE}.m.${EXT} -thumbnail ">x75" ${BASE}.t.${EXT} convert ${BASE}.t.${EXT} -thumbnail ">75" ${BASE}.t.${EXT} fi # Create the html for the medium image. if [ -f ${BASE}.html ]; then echo -n " HTML OK" > /dev/null; else echo -n " No HTML, creating..." echo "${TOP}" > ${BASE}.html cat .GALBREAD >> ${BASE}.html echo "${BASE}.html
BACK

\"${BASE}.m.${EXT}\"" >> ${BASE}.html echo "${BOTTOM}" >> ${BASE}.html fi # Create the html for the fullsize image. if [ $MATCH = Y ]; then ln -s ${BASE}.html ${BASE}full.html else if [ -f ${BASE}full.html ]; then echo " FULL HTML OK" > /dev/null; else echo " No FULL HTML, creating..." echo "${TOP}" > ${BASE}full.html cat .GALBREAD >> ${BASE}full.html echo "${BASE}full.html
BACK

\"${BASE}.${EXT}\"" >> ${BASE}full.html echo "${BOTTOM}" >> ${BASE}full.html fi fi # Make the html read-only chmod 444 ${BASE}.html ${BASE}full.html grep -v "${BASE}" .GAL > .GALTEMP && mv .GALTEMP .GAL # Add a link for this image to the index page echo "\"${BASE}.${EXT}\"" >> index.html echo "" # Make all images read-only so we're not rewriting them chmod 444 *.${EXT} done fi done # After all the images, insert links to files TEST=`wc -l .GAL | awk '{ print $1 }'` if [ ${TEST} -gt '1' ]; then echo -n '
' >> index.html echo '' >> index.html for FILE in `cat .GAL`; do DESCR=`file ${FILE} | awk -F ": " '{ print $2 }'` SDESCR=`echo ${DESCR} | cut -nc "1-41"` if [ `echo ${SDESCR} | wc -m` -gt '38' ] then SDESCR=${SDESCR}... fi SIZE=`ls -lh ${FILE} | awk '{ print $5 }'` MODIFY=`stat ${FILE} | grep "Modify" | cut -d " " -f "2"` SFILE=`echo ${FILE} | cut -c "1-40"` if [ `echo ${FILE} | wc -m` -gt '38' ] then SFILE=${SFILE}... fi echo -n "" >> index.html echo "" >> index.html done echo '
FileFiletypeSizeLast Modified
${SFILE}${SDESCR}${SIZE}bytes${MODIFY}
' >> index.html fi echo "${BOTTOM}" >> index.html # If there are subdirectories, descend into them, run this script, then back out. CHECK=$( find . -maxdepth "1" -type "d" | sed -e 's/\.\///g' | grep -v ^\\. | wc -l | awk '{ print $1 }' ) if [ ${CHECK} -gt '0' ]; then for DIR in `ls -l | grep ^drw | awk '{ print $9 }'`; do cd ${DIR} echo "Now working in `pwd`" /bin/sh $0 cd .. done # If the directories are read-only (or r-x), do not process them. for DIR in `ls -l | grep ^dr- | awk '{ print $9 }'`; do echo "WARNING: ${DIR} not processed, read-only." done # If no permissions to read directory, do not process them. for DIR in `ls -l | grep ^d--- | cut -d ":" -f "2" | awk '{ print $2 }'`; do echo "WARNING: ${DIR} not processed, unreadable." done fi rm -f ./.GAL*