Programming is not like cooking, where you can throw a bunch of things into a pot and get a delicious gumbo. Programming is like painting: the canvas may vary and what you're trying to draw will be different each day, but you need to work with the paints in a certain way, and if you try to throw them together then all you're going to be able to paint is "Brown Rock In Puddle of Mud". If you want red then you use red, and if you want yellow then you use yellow, and if you want some shade of orange then you mix red and yellow together in very precise, calculated, and controlled proportions.
You've got a lot of things going on here and only a small fraction of them are going to be appropriate, let alone useful, to your end goal. It's complicated enough that I'm not even entirely sure what your end goal is supposed to be (other than "you want a red dot").
So let's start at the beginning - or perhaps a little bit on this side of it.
You have a database, but let's pretend you already got what you need from it and now have that $b64 variable. In as technical terms as you know, what do you need to do with it? Do you need to output an <img> into your HTML that will show this image? Or do you already have the <img> and you need a script that you can reference to show the image?
And I'll skip ahead a bit and ask: in the general case, are these images going to be small and simple, or potentially large and detailed? Judging by the fact that you're talking about JPEGs that are 5"x5", I'm thinking the latter. We'll come back to that later.