I'm attempting to scan through my content and replace image source tags with something else (more notably, dataURIs when supported) - based on a couple of questions I've read through here, I'm trying preg_replace()
:
// Base64 Encodes an image
function wpdu_base64_encode_image($imagefile) {
$imgtype = array('jpg', 'gif', 'png');
$filename = file_exists($imagefile) ? htmlentities($imagefile) : die($imagefile.'Image file name does not exist');
$filetype = pathinfo($filename, PATHINFO_EXTENSION);
if (in_array($filetype, $imgtype)){
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
} else {
die ('Invalid image type, jpg, gif, and png is only allowed');
}
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
// Do the do
add_filter('the_content','wpdu_image_replace');
function wpdu_image_replace($content) {
$upload_dir = wp_upload_dir();
return preg_replace( '/<img.*src="(.*?)".*?>/', wpdu_base64_encode_image($upload_dir['path'].'/'.\1), $content );
}
The problem I'm running into is wpdu_base64_encode_image($upload_dir['path'].'/'.\1)
which is basically outputting the preg_replace
result - currently getting:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING
$upload_dir['path']
is correctly outputting the path to the image folder that I need, but there's also a few checks that I've tried and haven't been able to achieve as of yet:
- Check if the image source is relative and if it is, strip off the domain (currently can be done with
site_url()
which I'm assuming would need to be ofpreg_replace()
?) - If the image is not even local to the server (again - I'm assuming using the
site_url()
check), skip over it
I'm not as familiar with preg_replace()
if anyone has suggestions, I'd really appreciate it. Thanks!
Edit: Should I use http://simplehtmldom.sourceforge.net/ instead? Seems like a pretty-heavy hammer, but if that's a more reliable way then I'm all for it - anyone use that before?