Compare commits

..

6 Commits

Author SHA1 Message Date
Glenn Randers-Pehrson
1d9126cf0e [png2uri] Version 1.0.4, added check whether to use base64 or uuencode
depending on whether "uname" returns Linux or not.
2012-07-06 21:21:30 -05:00
Glenn Randers-Pehrson
35fbb6e6de [png2uri] Changed environment variable from "format" to "PNG2URI_FMT"
and cleaned up indentation.
2012-07-05 06:51:29 -05:00
Glenn Randers-Pehrson
9947a5f360 [png2uri] Added more references to other implementations 2012-07-04 15:03:13 -05:00
Glenn Randers-Pehrson
459827eab3 [png2uri] Added some references to other implementations. 2012-07-04 14:51:02 -05:00
Glenn Randers-Pehrson
8dec9119f0 [png2uri] Revised README to remove a TODO that's done in version 1.0.2. 2012-07-04 14:35:25 -05:00
Glenn Randers-Pehrson
9b96bff40c [png2uri] Added -f|--format option and added filetype detection from the
filename extension (default is PNG if no extension).
2012-07-04 14:30:22 -05:00
3 changed files with 217 additions and 60 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.
*/

169
png2uri
View File

@@ -1,6 +1,6 @@
#!/bin/sh
#
# png2uri version 1.0.1, July 4, 2012.
# png2uri version 1.0.4, July 6, 2012.
#
# NO COPYRIGHT RIGHTS ARE CLAIMED TO THIS SOFTWARE.
#
@@ -23,53 +23,164 @@
# 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".
#
# Requires /bin/sh and a uuencode(1) that takes the "-m" option to mean
# to encode in base64 according to RFC-3548.
# -u|--uri_only|--url_only
# omit the surrounding "img" tag and only write the
# data URI.
#
# Requires /bin/sh and a "base64" or "uuencode -m" that encodes its input
# in base64 according to RFC-3548 and writes the result on standard output,
# "uname", 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.
# it. Use your search engine and look for "data uri" to find one.
#
# I guess we need a "configure" script to figure out which of base64(1)
# or uuencode(1) to use, since neither is always present (uuencode must
# be installed as part of "sharutils" on Gnu/Linux platforms, which often
# hasn't happened). Here is a minimal "configure":
uri_only="false"
case X`uname` in
XLinux)
PNG2URI_BASE64=use_base64
;;
*)
PNG2URI_BASE64=use_uuencode
;;
esac
PNG2URI_FMT="unknown"
PNG2URI_URI="true"
while true
do
case x$1 in
x-u|x--ur*)
uri_only="true"
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)
X)
# Convert standard input.
case $uri_only in
true)
echo "data:image/png;base64,"
uuencode -m ==== | grep -v ====
;;
false)
echo "<img alt=\"PNG\" title=\"PNG\" src=\"data:image/png;base64,"
uuencode -m ==== | grep -v ====
echo "\">"
;;
case X$PNG2URI_FMT in
Xunknown)
PNG2URI_FMT=image/png
;;
esac
case X$PNG2URI_URI in
Xfalse)
echo "data:$PNG2URI_FMT;base64,"
case X$PNG2URI_BASE64 in
Xuse_base64)
base64
;;
Xuse_uuencode)
uuencode -m ==== | grep -v ====
;;
esac
;;
*)
echo "<img alt=\"PNG\" title=\"PNG\" src=\"data:$PNG2URI_FMT;base64,"
case X$PNG2URI_BASE64 in
Xuse_base64)
base64
;;
Xuse_uuencode)
uuencode -m ==== | grep -v ====
;;
esac
echo "\">"
;;
esac
exit
;;
*)
# Convert the named file.
case $uri_only in
true)
echo "data:image/png;base64,"
uuencode -m $1 ==== | grep -v ====
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,"
case X$PNG2URI_BASE64 in
Xuse_base64)
base64 < $1
;;
Xuse_uuencode)
uuencode -m $1 ==== | grep -v ====
;;
esac
;;
false)
echo "<img alt=\"$1\" title=\"$1\" src=\"data:image/png;base64,"
uuencode -m $1 ==== | grep -v ====
*)
echo "<img alt=\"$1\" title=\"$1\" src=\"data:$PNG2URI_FMT;base64,"
case X$PNG2URI_BASE64 in
Xuse_base64)
base64 < $1
;;
Xuse_uuencode)
uuencode -m $1 ==== | grep -v ====
;;
esac
echo "\">"
;;
esac

View File

@@ -21,16 +21,31 @@
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".
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
(3 out of 9) don't have uuencode installed.
-u|--uri_only|--url_only
omit the surrounding "img" tag and only write the
data URI.
Requires /bin/sh and a base64(1) that encodes its input in base64
according to RFC-3548 and writes the result on standard output, and
a working "sed". Versions 1.0.0 through 1.0.3 used uuencode(1) instead
of base64(), but a surprising number of machines that I've tried
(5 out of 11) don't have uuencode installed, while all 11 had base64(1)
which is a "core utility" rather than a "shar utility". Using base64()
has an additional advantage that the lines are always folded to the same
width, 76 characters wide, while uuencode sometimes writes shorter lines.
REFERENCES:
http://en.wikipedia.org/wiki/Data_URI_scheme
@@ -47,6 +62,23 @@
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.
Here are some:
A PHP script:
http://css-tricks.com/snippets/php/create-data-uris/
Hixie's web-based converter
http://software.hixie.ch/utilities/cgi/data/data/
Websemantics' web-based converter
http://websemantics.co.uk/online_tools/image_to_data_uri_convertor
Mike Scalora's web-based converter
http://www.scalora.org/projects/uriencoder/
Data URL Maker (a web-based drag-and-drop converter), CSS Optimizer,
and Data URL Toolkit (a PERL implementation)
http://dataurl.net/#about
CHANGE LOG
@@ -62,52 +94,72 @@
Implemented "-u" and "--url_only" option.
Version 1.0.2, July 4, 2012:
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.
Version 1.0.4, July 6, 2012:
Use "base64" instead of "uuencode" when "uname" returns "Linux" to
generate the output, because "base64" seems to be more prevalently
available. On Gnu/Linux platforms, uuencode must be installed as part
of "sharutils", which often doesnn't happen. SunOS and FreeBSD don't
supply "base64" so we use "uuencode" instead.
TO DO
1. Test on various platforms. The following have been tried
1. Test on various other platforms. I assume we'll soon find one that has
neither "uuencode" nor "base64". The following have been tried
Ubuntu 10.04 (data is folded to 60 characters)
Ubuntu 10.04 (uuencode output was folded to 60 characters)
Linux glenn.rp 2.6.32-41-generic #91-Ubuntu SMP Wed Jun 13 11:43:55
UTC 2012 x86_64 GNU/Linux
Linux nancy.rp 2.6.32-41-generic #91-Ubuntu SMP Wed Jun 13 11:43:55
UTC 2012 x86_64 GNU/Linux (uuencode not found. Fixed that with
"sudo aptitude install sharutils", then data folded to 60 chars)
"sudo aptitude install sharutils", then finally fixed by using
base64() instead of uuencode())
Ubuntu 12.04 (FAILED: uuencode not found but manpage for POSIX uuencode
exists)
Ubuntu 12.04 (uuencode not found but manpage for POSIX uuencode exists;
base64 works)
Linux scooby 3.0.0-22-generic #36-Ubuntu SMP Tue Jun 12 17:37:42 UTC 2012
x86_64 x86_64 x86_64 GNU/Linux
SunOS 5.10 (data is folded to 68 characters)
SunOS 5.10 (uuencode data is folded to 68 characters)
Does not work with "base64"! Reconfigured png2uri to use "uuencode".
on non-Linux platforms.
SunOS freddy 5.10 Generic_147441-13 i86pc i386 i86pc
SunOS blade 5.10 Generic_144488-12 sun4u sparc SUNW,Sun-Blade-2500
Sunos 5.11 (data is folded to 68 characters)
Sunos 5.11 (uuencode data is folded to 68 characters)
SunOS weerd 5.11 oi_151a4 i86pc i386 i86pc
FreeBSD 8.0 (data is folded to 76 characters)
FreeBSD 8.0 (uuencode data is folded to 76 characters)
"base64 not found"; use uuencode.
FreeBSD shaggy.simplesystems.org 8.0-RELEASE-p1 FreeBSD 8.0-RELEASE-p1
#1: Mon Dec 21 11:26:14 CST 2009
zsh@shaggy.simplesystems.org:/usr/src/sys/i386/compile/GENERIC
i386
Red Hat? (FAILED: uuencode not found but manpage for POSIX uuencode
exists)
Red Hat? (uuencode not found but manpage for POSIX uuencode
exists; worked when base64 was used instead of uuencode)
Linux studio.imagemagick.org 2.6.18-308.4.1.el5xen #1 SMP Tue Apr 17
17:49:15 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux
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
Two other Redhat Enterprise platforms also worked with base64, but
uuencode was not installed.
2. This script can be trivially modified to support another image format
(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.
3. Find out if the script works on Windows and Mac or can be modified to
2. Find out if the script works on Windows and Mac or can be modified to
do so.
4. Make it work as a drag-and-drop application.
3. Make it work as a drag-and-drop application.
5. But keep it simple!
4. But keep it simple!
*/