[png2uri] Added -f|--format option and added filetype detection from the

filename extension (default is PNG if no extension).
This commit is contained in:
Glenn Randers-Pehrson
2012-07-04 14:30:22 -05:00
parent ca7f016b9e
commit 9b96bff40c
3 changed files with 85 additions and 22 deletions

View File

@@ -1,6 +0,0 @@
/*
This is the master branch of the "pmt" tree.
Individual projects are in separate branches,
e.g. pngcrush is in the "pngcrush" branch.
*/

73
png2uri
View File

@@ -1,6 +1,6 @@
#!/bin/sh
#
# png2uri version 1.0.1, July 4, 2012.
# png2uri version 1.0.2, July 4, 2012.
#
# NO COPYRIGHT RIGHTS ARE CLAIMED TO THIS SOFTWARE.
#
@@ -23,37 +23,75 @@
# 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 [-u|--uri_only] [file]
# Usage: png2uri [-f type|--format type] [-u|--uri_only] [file]
#
# options: -u|--uri_only|--url_only: omit the surrounding "img" tag
# and only write the data URI.
# 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.
# 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 "png data uri" to find one.
#
uri_only="false"
format="unknown"
while true
do
case x$1 in
x-f|x--format)
shift
case x$1 in
xpng|xPNG)
format=image/png
;;
xjpeg|xJPEG|xjpg|xJPG)
format=image/jpeg
;;
xbmp|xBMP)
format=image/bmp
;;
xgif|xGIF)
format=image/gif
;;
*)
format=$1
;;
esac
shift
;;
x-u|x--ur*)
uri_only="true"
shift
;;
x)
# Convert standard input.
case $format in
unknown)
format=image/png
;;
esac
case $uri_only in
true)
echo "data:image/png;base64,"
echo "data:$format;base64,"
uuencode -m ==== | grep -v ====
;;
false)
echo "<img alt=\"PNG\" title=\"PNG\" src=\"data:image/png;base64,"
echo "<img alt=\"PNG\" title=\"PNG\" src=\"data:$format;base64,"
uuencode -m ==== | grep -v ====
echo "\">"
;;
@@ -62,13 +100,30 @@ case x$1 in
;;
*)
# Convert the named file.
case $format in
unknown)
format=image/png
extension=`echo $1 | sed "s/.*\.//"`
case x$extension in
xjpeg|xJPEG|xjpg|xJPG)
format=image/jpeg
;;
xbmp|xBMP)
format=image/bmp
;;
xgif|xGIF)
format=image/gif
;;
esac
;;
esac
case $uri_only in
true)
echo "data:image/png;base64,"
echo "data:$format;base64,"
uuencode -m $1 ==== | grep -v ====
;;
false)
echo "<img alt=\"$1\" title=\"$1\" src=\"data:image/png;base64,"
echo "<img alt=\"$1\" title=\"$1\" src=\"data:$format;base64,"
uuencode -m $1 ==== | grep -v ====
echo "\">"
;;

View File

@@ -21,12 +21,22 @@
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
input.
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 [-u|--uri_only] [file]
Usage: png2uri [-f format|--format format] [-u|--uri_only] [file]
options: -u|--uri_only|--url_only: omit the surrounding "img" tag
and only write the data URI.
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. A surprising number of machines that I've tried
@@ -62,6 +72,10 @@
Implemented "-u" and "--url_only" option.
Version 1.0.2, July 4, 2012:
Implemented "-f TYPE" and "--format TYPE" option.
TO DO
1. Test on various platforms. The following have been tried
@@ -98,10 +112,10 @@
Linux magick.imagemagick.org 3.4.4-3.fc17.x86_64 #1 SMP Tue Jun 26
20:54:56 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
2. This script can be trivially modified to support another image format
2. This script can be trivially modified to support any other MIME type
(e.g., change PNG to JPG and "image/png" to "image/jpeg" throughout).
To do: do that, via a "-f/--format jpg|jpeg|png|bmp|gif" option and by
inspecting the filename extension.
To do: do that, via a "-f/--format jpg|jpeg|png|bmp|gif" option (done
as of version 1.0.2) and by inspecting the filename extension (still to do).
3. Find out if the script works on Windows and Mac or can be modified to
do so.