It appears to be an issue with your RegEx pattern matching images.
I modified $pattern
to the following and it worked on a test page I setup.
$pattern = '/<img(.*?)src="https://wordpress.stackexchange.com/questions/114747/(.*?).(bmp"gif|jpeg|jpg|png)"(.*?)width="(.*?)" height="(.*?)" \/>/i';
There were some extra spaces which were throwing it off.
I suggest using a RegEx tester to help you make sure your pattern is correctly matching your content coming out of wordpress. This is the one I used to check your pattern:
On a separate note, in general it’s a bit hard to tell what you’re trying to achieve with the two preg_replace functions. I’m sure it could be significantly simplified…