From b8a41e3ec8caad9237e685539ae5ef3be650a734 Mon Sep 17 00:00:00 2001 From: Glenn Randers-Pehrson Date: Sat, 7 Jul 2012 13:52:38 -0500 Subject: [PATCH] [png2uri] Version 1.0.5, July 7, 2012: Eliminated use of "uname" and use the return of "base64" and "uuencode" to select the workable one (checking "base64" first); suggested by John Bowler. Added a check for too many arguments, also a JB suggestion. Accept output filename as an optional final argument. --- png2uri | 83 ++++++++++++++++++---------------------------- png2uri-README.txt | 53 +++++++++-------------------- 2 files changed, 48 insertions(+), 88 deletions(-) diff --git a/png2uri b/png2uri index d14ffdcfd..0c239836e 100755 --- a/png2uri +++ b/png2uri @@ -1,6 +1,6 @@ #!/bin/sh # -# png2uri version 1.0.4, July 6, 2012. +# png2uri version 1.0.5, July 7, 2012. # # NO COPYRIGHT RIGHTS ARE CLAIMED TO THIS SOFTWARE. # @@ -18,7 +18,8 @@ # file. None of the people below claim any rights with regard to the # contents of this file. # -# Glenn Randers-Pehrson +# Glenn Randers-Pehrson is the author. +# John Bowler contributed some useful suggestions. # # 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 @@ -27,7 +28,7 @@ # "--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] +# Usage: png2uri [-f type|--format type] [-u|--uri_only] [infile [outfile]] # # options: -f|--format TYPE: # write "image/TYPE" instead of "image/png" in the @@ -42,24 +43,18 @@ # # 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". +# 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. # -# 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": +# This script figures 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). -case X`uname` in - XLinux) - PNG2URI_BASE64=use_base64 - ;; - *) - PNG2URI_BASE64=use_uuencode - ;; -esac +png2uri_encode(){ +base64 2>/dev/null || uuencode -m "====" 2>/dev/null | grep -v "====" +} PNG2URI_FMT="unknown" PNG2URI_URI="true" @@ -110,26 +105,12 @@ case X$1 in 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 + png2uri_encode ;; *) echo "\"PNG\"" ;; esac @@ -138,12 +119,28 @@ case X$1 in *) # Convert the named file. + + # First make sure there weren't extra arguments + case $# in + 1) + : # OK, continue + ;; + 2) + exec < "$1" > "$2" + ;; + *) + echo "png2uri: too many arguments: $*" >&2 + echo "usage: png2uri [options] [infile [outfile]" >&2 + exit 1 + ;; + esac + case X$PNG2URI_FMT in Xunknown) PNG2URI_FMT=image/png - extension=`echo $1 | sed "s/.*\.//"` + png2uri_extension=`echo $1 | sed "s/.*\.//"` - case X$extension in + case X$png2uri_extension in Xjpeg|XJPEG|Xjpg|XJPG) PNG2URI_FMT=image/jpeg ;; @@ -162,25 +159,11 @@ case X$1 in 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 + png2uri_encode < "$1" ;; *) echo "\"$1\"" ;; esac diff --git a/png2uri-README.txt b/png2uri-README.txt index c91d7c0af..9e846a665 100644 --- a/png2uri-README.txt +++ b/png2uri-README.txt @@ -25,7 +25,7 @@ or "--format" option or, if that option was not supplied, by inspection of the filename extension. - Usage: png2uri [-f format|--format format] [-u|--uri_only] [file] + Usage: png2uri [-f format|--format format] [-u|--uri_only] [infile [outfile]] options: -f|--format TYPE: write "image/TYPE" instead of "image/png" in the @@ -112,48 +112,25 @@ of "sharutils", which often doesnn't happen. SunOS and FreeBSD don't supply "base64" so we use "uuencode" instead. + Version 1.0.5, July 7, 2012: + + Eliminated use of "uname" and use the return of "base64" and "uuencode" + to select the workable one (checking "base64" first); suggested by + John Bowler. Added a check for too many arguments, also a JB suggestion. + Accept output filename as an optional final argument. + TO DO 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 (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 finally fixed by using - base64() instead of uuencode()) - - 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 (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 (uuencode data is folded to 68 characters) - SunOS weerd 5.11 oi_151a4 i86pc i386 i86pc - - 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? (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. + Ubuntu 10.04 (using "base64") + Ubuntu 12.04 (using "base64") + SunOS 5.10 (using "uuencode") + SunOS 5.11 (using "uuencode") + FreeBSD 8.0 (using "uuencode") + Redhat Gnu/Linux (using "base64") + Redhat Enterprise (using "base64") 2. Find out if the script works on Windows and Mac or can be modified to do so.