[png2uri] Changed environment variable from "format" to "PNG2URI_FMT"

and cleaned up indentation.
This commit is contained in:
Glenn Randers-Pehrson
2012-07-05 06:51:29 -05:00
parent 9947a5f360
commit 35fbb6e6de
2 changed files with 87 additions and 67 deletions

144
png2uri
View File

@@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# #
# png2uri version 1.0.2, July 4, 2012. # png2uri version 1.0.3, July 5, 2012.
# #
# NO COPYRIGHT RIGHTS ARE CLAIMED TO THIS SOFTWARE. # NO COPYRIGHT RIGHTS ARE CLAIMED TO THIS SOFTWARE.
# #
@@ -23,8 +23,8 @@
# png2uri is a command-line application that creates an HTML "img" tag on # 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 # standard output containing a data URI in accordance with RFC-2397, from
# a PNG file or from a PNG datastream received from standard input. # a PNG file or from a PNG datastream received from standard input.
# Other formats besides the default, PNG, are supported, via the "-f or # 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 # "--format" option or, if that option was not supplied, by inspection of the
# filename extension. # filename extension.
# #
# Usage: png2uri [-f type|--format type] [-u|--uri_only] [file] # Usage: png2uri [-f type|--format type] [-u|--uri_only] [file]
@@ -44,86 +44,100 @@
# to encode in base64 according to RFC-3548, and a working "sed". # 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 # If you prefer a web-based converter or a java application, this isn't
# it. Use your search engine and look for "png data uri" to find one. # it. Use your search engine and look for "data uri" to find one.
# #
uri_only="false" PNG2URI_URI="true"
format="unknown" PNG2URI_FMT="unknown"
while true while true
do do
case x$1 in case X$1 in
x-f|x--format) X-f|X--format)
shift shift
case x$1 in
xpng|xPNG) case X$1 in
format=image/png Xpng|XPNG)
;; PNG2URI_FMT=image/png
xjpeg|xJPEG|xjpg|xJPG) ;;
format=image/jpeg
;; Xjpeg|XJPEG|Xjpg|XJPG)
xbmp|xBMP) PNG2URI_FMT=image/jpeg
format=image/bmp ;;
;;
xgif|xGIF) Xbmp|XBMP)
format=image/gif PNG2URI_FMT=image/bmp
;; ;;
*)
format=$1 Xgif|XGIF)
;; PNG2URI_FMT=image/gif
esac ;;
*)
PNG2URI_FMT=$1
;;
esac
shift
;;
X-u|X--ur*)
PNG2URI_URI="false"
shift shift
;; ;;
x-u|x--ur*)
uri_only="true" X)
shift
;;
x)
# Convert standard input. # Convert standard input.
case $format in case X$PNG2URI_FMT in
unknown) Xunknown)
format=image/png PNG2URI_FMT=image/png
;; ;;
esac esac
case $uri_only in
true) case X$PNG2URI_URI in
echo "data:$format;base64," Xfalse)
uuencode -m ==== | grep -v ==== echo "data:$PNG2URI_FMT;base64,"
;; uuencode -m ==== | grep -v ====
false) ;;
echo "<img alt=\"PNG\" title=\"PNG\" src=\"data:$format;base64,"
uuencode -m ==== | grep -v ==== *)
echo "\">" echo "<img alt=\"PNG\" title=\"PNG\" src=\"data:$PNG2URI_FMT;base64,"
;; uuencode -m ==== | grep -v ====
echo "\">"
;;
esac esac
exit exit
;; ;;
*) *)
# Convert the named file. # Convert the named file.
case $format in case X$PNG2URI_FMT in
unknown) Xunknown)
format=image/png PNG2URI_FMT=image/png
extension=`echo $1 | sed "s/.*\.//"` extension=`echo $1 | sed "s/.*\.//"`
case x$extension in
xjpeg|xJPEG|xjpg|xJPG) case X$extension in
format=image/jpeg Xjpeg|XJPEG|Xjpg|XJPG)
;; PNG2URI_FMT=image/jpeg
xbmp|xBMP) ;;
format=image/bmp
;; Xbmp|XBMP)
xgif|xGIF) PNG2URI_FMT=image/bmp
format=image/gif ;;
;;
Xgif|XGIF)
PNG2URI_FMT=image/gif
;;
esac esac
;; ;;
esac esac
case $uri_only in
true) case X$PNG2URI_URI in
echo "data:$format;base64," Xfalse)
echo "data:$PNG2URI_FMT;base64,"
uuencode -m $1 ==== | grep -v ==== uuencode -m $1 ==== | grep -v ====
;; ;;
false) *)
echo "<img alt=\"$1\" title=\"$1\" src=\"data:$format;base64," echo "<img alt=\"$1\" title=\"$1\" src=\"data:$PNG2URI_FMT;base64,"
uuencode -m $1 ==== | grep -v ==== uuencode -m $1 ==== | grep -v ====
echo "\">" echo "\">"
;; ;;

View File

@@ -21,8 +21,8 @@
png2uri is a command-line application that creates an HTML "img" tag on png2uri is a command-line application that creates an HTML "img" tag on
standard output containing a data URI, from a PNG file or from standard standard output containing a data URI, from a PNG file or from standard
input. Other formats besides the default, PNG, are supported, via the "-f 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 or "--format" option or, if that option was not supplied, by inspection of
the filename extension. the filename extension.
Usage: png2uri [-f format|--format format] [-u|--uri_only] [file] Usage: png2uri [-f format|--format format] [-u|--uri_only] [file]
@@ -93,6 +93,12 @@
Implemented "-f TYPE" and "--format TYPE" option. Implemented "-f TYPE" and "--format TYPE" option.
Version 1.0.3, July 5, 2012:
Changed environment variable "format" to PNG2URI_FMT reduce possibility`
of name conflict, and changed "uri_only" to PNG2URI_URI. Fixed some
indentation.
TO DO TO DO
1. Test on various platforms. The following have been tried 1. Test on various platforms. The following have been tried