How to add filter the post thumbnail before save in database?

Sorry for the delay – crazy day here…

So I looked at this and tested a few things. I think the sexiest way to do this is to try and return your base64 encoded img src at the… source. (pun not intended)

Have a look at the image_downsize filter – I believe you can do it in there, either on demand or, if performance becomes an issue, you could then cache your strings in the db. (so first to ask for an image generates the base64 string, and saves it in the db)

My initial tests are conclusive here – this will manage both images added to content through the WYSIWYG editor, and images included by theme functions.

By the way, if you are interested and allow me to re-use your encoding code, I’ll gladly make this into an open source plugin…

Oh, and one last comment: you have a small mistake in your base64 string:

$src="https://wordpress.stackexchange.com/questions/253180/data:".$filetype['ext'].';base64,'.$imageData;

should be

$src="data:image/".$filetype['ext'].';base64,'.$imageData;

Hope this helps!