Skip to main content

These can be useful for embedding images into HTML/CSS/JS to save on HTTP requests, at the cost of maintainability.

/**
 * Create Data URI's
 *
 * These can be useful for embedding images into HTML/CSS/JS to
 * save on HTTP requests, at the cost of maintainability.
 *
 * More information. There are online tools to do it, but if you
 * want your own very simple utility, here's some PHP to do it.
 *
 * @param string $file
 * @param string $mime
 * @return string
 */
function dataUri($file, $mime) {
    $contents = file_get_contents($file);
    $base64 = base64_encode($contents);

    return "data:$mime;base64,$base64";
}