Skip to main content

Bash function that creates a data URI scheme for a file.

#!/usr/bin/env bash

# generate data uri scheme for file
function datauri()
{
    local mimeType=$(file -b --mime-type "$1")
    if [[ $mimeType == text/* ]]; then
        mimeType="${mimeType};charset=utf-8"
    fi
    echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
}