I don't know about the discrete GPUs in those old iMacs, but if I remember correctly, the integrated GPUs in the Ivy Bridge CPUs did not originally have the ability to drive 4K displays.
Intel came out with a driver to allow the iGPUs in those CPUs to drive a 4K display, but the driver wasn't of much consolation to many PC owners. Even with the driver, the processor lacked the horsepower to drive a 4K display using a single iGPU output. A computer had to be wired to use two of an Ivy Bridge CPU's iGPU outputs to paint one 4K display – "which unfortunately [made] it so [that] most existing Ivy Bridge systems [weren't] able to drive the higher resolution panels."
https://www.anandtech.com/show/6270/ivy-bridge-gets-4k-display-support-in-october
If you could build a new custom logic board to wire two iGPU outputs to a 4K panel – no easy feat, and a definite way to blow the cost of the project completely out of the water – and you could get macOS to recognize it (good luck with that!) – you might find that you were not terribly happy with the speed of that old CPU/iGPU. Driving a Retina display is like driving four non-Retina ones. Some of the first Retina Macs had performance issues around pushing that many pixels, and here you'd be trying it with an older CPU/iGPU.
Other issues are that a 2012 "Franken-iMac" would
- Likely be hard to service even by repair shops that still service obsolete Macs
- Be limited to Catalina - limiting the ability to run modern applications (no Microsoft 365, no Adobe anything)
- Probably be running off a 5400 rpm 2.5" hard drive instead of a super-fast SSD
At some point, you say, "It's not worth it. Time to buy a current, or much newer, machine that will have updated *everything*."