In order to enhance the user experience on my website, I am looking to implement a feature that dynamically changes the background images based on the user's screen resolution.
My plan is to use a small snippet of JavaScript within the <head>
section that will determine the screen resolution and set the background image using CSS accordingly. For instance, the image URL would be something like
http://example.com/backgrounds/beach_800x600
, with the dimensions being determined by the script.
To ensure that most users get an optimal experience, I am preparing various resized images for common screen resolutions. However, in cases where there is no exact match for a specific resolution, I plan to dynamically create and resize an image on the fly to accommodate that resolution. The goal is to limit the number of custom images created to avoid excessive server load.
My concerns are:
1) Is this method considered safe and efficient? I want to avoid creating more than 50 custom images for unique screen resolutions. Are there any potential security risks associated with this approach that I should be aware of?
2) Should I incorporate an error margin of, for example, 50 or 100 pixels to accommodate resolutions that do not have an exact match? This would allow me to serve existing images that are close in size, such as serving a 600x800 image for a 700x900 resolution. If implementing an error margin, what would be the optimal number of pixels to set it at?