Get thumbnail image according to the screen

You should not attempt to know the user device on server side, as this kind of thing will mean you will not be able to use any page caching, and it will fail when the browser “window” size changes.

Do such decisions on client side. If it is impossible to simply use CSS, just prepare an appropriate JS array with the URLS of possible images and set the images to the appropriate one based on actual client area detection done in JS.