Skip to content

Instantly share code, notes, and snippets.

@Kolin
Created January 21, 2016 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kolin/f2b9b06166de31ee11b6 to your computer and use it in GitHub Desktop.
Save Kolin/f2b9b06166de31ee11b6 to your computer and use it in GitHub Desktop.
<?php
function data_uri($filename) {
$mime = mime_content_type($filename);
$data = base64_encode(file_get_contents($filename));
return "data:$mime;base64,$data";
}
$content = file_get_contents('oldfile.html');
$search = '/(<img\s+src=["\'])([^"\']+)(["\']\s+[^>]+>)/';
$content = preg_replace_callback($search, create_function(
'$matches',
'return $matches[1] . data_uri($matches[2]) . $matches[3];'
), $content);
echo $content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment