blob: 8feba9d545c225ea21f95a0c82fafa7f4535e43d [file] [log] [blame]
#!/bin/sh
#
# png2uri version 1.0.3, July 5, 2012.
#
# NO COPYRIGHT RIGHTS ARE CLAIMED TO THIS SOFTWARE.
#
# To the extent possible under law, the author has waived all copyright and
# related or neighboring rights to this work. This work is published from
# the United States of America in 2012.
#
# This png2uri software may be used freely in any way.
#
# The source is the original work of the person named below. No other person
# or organization has made contributions to this work.
#
# ORIGINAL AUTHORS
# The following people have contributed to the code or comments in this
# file. None of the people below claim any rights with regard to the
# contents of this file.
#
# Glenn Randers-Pehrson <glennrp@users.sourceforge.net>
#
# png2uri is a command-line application that creates an HTML "img" tag on
# standard output containing a data URI in accordance with RFC-2397, from
# a PNG file or from a PNG datastream received from standard input.
# Other formats besides the default, PNG, are supported, via the "-f" or
# "--format" option or, if that option was not supplied, by inspection of the
# filename extension.
#
# Usage: png2uri [-f type|--format type] [-u|--uri_only] [file]
#
# options: -f|--format TYPE:
# write "image/TYPE" instead of "image/png" in the
# data uri. TYPE can be png, jpg, jpeg, bmp, or gif,
# or any of those in upper case. To write any other
# type, include the complete MIME type as in
# "--format image/x-jng" or "-f audio/mpeg".
#
# -u|--uri_only|--url_only
# omit the surrounding "img" tag and only write the
# data URI.
#
# Requires /bin/sh and a uuencode(1) that takes the "-m" option to mean
# to encode in base64 according to RFC-3548, and a working "sed".
#
# If you prefer a web-based converter or a java application, this isn't
# it. Use your search engine and look for "data uri" to find one.
#
PNG2URI_URI="true"
PNG2URI_FMT="unknown"
while true
do
case X$1 in
X-f|X--format)
shift
case X$1 in
Xpng|XPNG)
PNG2URI_FMT=image/png
;;
Xjpeg|XJPEG|Xjpg|XJPG)
PNG2URI_FMT=image/jpeg
;;
Xbmp|XBMP)
PNG2URI_FMT=image/bmp
;;
Xgif|XGIF)
PNG2URI_FMT=image/gif
;;
*)
PNG2URI_FMT=$1
;;
esac
shift
;;
X-u|X--ur*)
PNG2URI_URI="false"
shift
;;
X)
# Convert standard input.
case X$PNG2URI_FMT in
Xunknown)
PNG2URI_FMT=image/png
;;
esac
case X$PNG2URI_URI in
Xfalse)
echo "data:$PNG2URI_FMT;base64,"
uuencode -m ==== | grep -v ====
;;
*)
echo "<img alt=\"PNG\" title=\"PNG\" src=\"data:$PNG2URI_FMT;base64,"
uuencode -m ==== | grep -v ====
echo "\">"
;;
esac
exit
;;
*)
# Convert the named file.
case X$PNG2URI_FMT in
Xunknown)
PNG2URI_FMT=image/png
extension=`echo $1 | sed "s/.*\.//"`
case X$extension in
Xjpeg|XJPEG|Xjpg|XJPG)
PNG2URI_FMT=image/jpeg
;;
Xbmp|XBMP)
PNG2URI_FMT=image/bmp
;;
Xgif|XGIF)
PNG2URI_FMT=image/gif
;;
esac
;;
esac
case X$PNG2URI_URI in
Xfalse)
echo "data:$PNG2URI_FMT;base64,"
uuencode -m $1 ==== | grep -v ====
;;
*)
echo "<img alt=\"$1\" title=\"$1\" src=\"data:$PNG2URI_FMT;base64,"
uuencode -m $1 ==== | grep -v ====
echo "\">"
;;
esac
exit
;;
esac
done