I'm trying to find a way to locate all of the images on a page and modify their source as needed. Here is what I have so far:
add_filter('the_content','wpdu_image_replace');
function wpdu_image_replace($content) {
$upload_dir = wp_upload_dir();
$pattern = '/<img.*src="(.*?)".*?>/';
$replacement = wpdu_base64_encode_image($upload_dir['path'].'/'.\1);
return preg_replace( $pattern, $replacement , $content );
}
I have three problems though:
- I'm starting off the 'src' tag using the relative path on the server -- but there is no check to see if:
- The image does actually exist on the server
- If the image does, is the URL given relative or not
- My
$replacement
variable is wrong (I'm not sure how to output what is only in the src tag) - I'd like to not have to declare the
<img>
tag in the replacement, because then I'd lose everything else that was surrounding it (like classes, ids, etc).
Does anyone know how to grab just the source of images and replace that in the fashion I'm describing? I've looked at Simple HTML DOM as an alternative -- but was getting horrible performance results. Any help would be greatly appreciated. Thanks!