Skip to content

Instantly share code, notes, and snippets.

@takumakei
Last active December 26, 2015 23:38
Show Gist options
  • Save takumakei/7231469 to your computer and use it in GitHub Desktop.
Save takumakei/7231469 to your computer and use it in GitHub Desktop.
[macosx] a shell script to create a data-uri from an image file.
#!/bin/bash
function get-property {
sips -g "$1" "$2" | tail -1 | awk '{ print $2 }'
}
function col-80 {
ruby -e "
b = []
while a = gets
b << a.strip
end
b = b.join
while b
puts b[0, 80]
b = b[80, b.size]
end
"
}
function encode-base64 {
openssl base64 < "$1" | col-80
}
function duri {
f="$1"
N=$(basename "$f")
W=$(get-property pixelWidth "$f")
H=$(get-property pixelHeight "$f")
F=$(get-property format "$f")
D=$(encode-base64 "$f")
echo "<image
width='$W' height='$H'
title='$N' alt='$N'
src='data:image/$F;base64,
$D'>"
}
function msgbox {
M="$*"
osascript -e "
try
tell application \"SystemUIServer\"
set answer to display dialog \"$M\" with title \"DataURI\" buttons { \"OK\" } default button 1
end
end
activate app (path to frontmost application as text)
"
}
function main {
N=$(basename "$1")
D=$(duri "$1")
L=$(echo "$D" | wc -l | awk '{ print $1 }')
echo "$D" | pbcopy
if [ $# -gt 1 ]; then
msgbox "$N
text of $L lines copied to clipboard
( You must not drop multiple files at once. )"
else
msgbox "$N
text of $L lines copied to clipboard"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment