Last active
December 15, 2015 05:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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