Skip to content

Instantly share code, notes, and snippets.

@hagronnestad
Last active December 15, 2015 05:49
Show Gist options
  • Save hagronnestad/5211866 to your computer and use it in GitHub Desktop.
Save hagronnestad/5211866 to your computer and use it in GitHub Desktop.
Converts all img src URLs to base64 data urls. Very crude, doesn't check the MIME type, probably som regex issues to.
function URLtoData($html) {
$imgTags = null;
preg_match_all("/<img.*?>/", $html, $imgTags);
foreach ($imgTags[0] as $imgTag) {
$src = null;
preg_match_all("/src=\"([^\"]*)/", $imgTag, $src);
$src = $src[1][0];
$data = base64_encode(file_get_contents($src));
$html = str_replace($src, "data:image/jpg;base64,{$data}", $html);
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment