#!/bin/sh # j2pa CGI script # give it query params url= and (optionally) width= # it will produce a text version of the image specified # it will cache results in $cache_dir # it requires: # curl from http://curl.haxx.se/ # convert from http://www.imagemagick.org/ # jp2a from http://jp2a.sourceforge.net/ PATH=/home/childrf6/bin:$PATH cache_dir='/home/childrf6/public_html/jp2a/' jp2a_args='--background=light' echo -e 'Content-Type: text/plain\n\n' default_width=600 max_width=800 min_width=1 case "$QUERY_STRING" in [0-9]/*|[0-9][0-9]/*|[0-9][0-9][0-9]/*) width=${QUERY_STRING%%/*} url=${QUERY_STRING#*/} ;; http://*|https://*) url=${QUERY_STRING} ;; *) while test -n "$QUERY_STRING"; do arg="${QUERY_STRING%%&*}" QUERY_STRING="${QUERY_STRING#$arg}" QUERY_STRING="${QUERY_STRING#&}" case "$arg" in url=*) url="${arg#url=}";; width=*) width="${arg#width=}";; esac done ;; esac let 'width>=min_width && width<=max_width' || width=$default_width hash="$(echo "$url"|md5sum)" cache="$cache_dir"/${hash%% *}-$width.txt.gz case "$url" in http://*|https://*) if [ -f "$cache" ]; then exec gzip -cd "$cache" else # racey if curl -s "$url" | convert - jpg:- | jp2a --width=$width $jp2a_args - | tee "${cache%.gz}"; then gzip "${cache%.gz}" else rm "${cache%.gz}" fi fi ;; *) echo 'no url or bad url.' ;; esac