Skip to content

Instantly share code, notes, and snippets.

@ryanj
Created November 21, 2014 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanj/e33185cdeef121863012 to your computer and use it in GitHub Desktop.
Save ryanj/e33185cdeef121863012 to your computer and use it in GitHub Desktop.
encode assets to be loaded inline as dataURIs
<?php
function base64DataUri($sFile)
{
// Switch to right MIME-type
$sExt = strtolower(substr(strrchr($sFile, '.'), 1));
switch($sExt)
{
case 'gif':
case 'jpg':
case 'png':
$sMimeType = 'image/'. $sExt;
break;
case 'ico':
$sMimeType = 'image/x-icon';
break;
case 'eot':
$sMimeType = 'application/vnd.ms-fontobject';
break;
case 'otf':
case 'ttf':
case 'woff':
$sMimeType = 'application/octet-stream';
break;
default:
exit('Invalid extension file!');
}
$sBase64 = base64_encode(file_get_contents($sFile));
return "data:$sMimeType;base64,$sBase64";
}
$file = 'static/font/overpass_bold-webfont.woff';
print base64DataUri($file);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment