在服务器端检测屏幕尺寸和像素密度?
我一直在做一些研究,我想我已经知道答案了,但我想知道是否有任何方法可以在不使用 javascript 或依赖 CSS3 媒体查询的情况下获取设备的屏幕尺寸和像素密度。
本质上,我正在研究如何获取屏幕分辨率和像素密度,以便服务器可以决定在 URI 请求中服务器哪个图像。
到目前为止,我还没有发现任何东西表明这是可能的,但我想嘿,为什么不问呢?
I've been doing some research and I think I know the answer already, but I'm wondering if there's any means by which you can get a device's screen size and pixel density without the use of javascript or relying on CSS3 media queries.
Essentially, I'm looking into what it would take to get the screen resolution and pixel density so that the server could decide which image to server in a URI request.
So far I've not found anything that says this is even possible but I thought hey, why not ask?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不完全同意上面的正确答案。实际上,这个答案在很多情况下都是正确的……但理论上却不然。通常,向 Web 服务器发出的请求包含用户代理字段,理论上,该字段可用于识别有关设备屏幕分辨率和属性的信息。
Web 请求不会首先通过客户端。它们传递到服务器,然后服务器向客户端提供页面,以便服务器首先获取请求...例如,Ruby on Rails 通过操作控制器接收对资源的请求,然后向响应提供页面。
查看 UA 解析器示例,例如: https://github.com/visionmedia/user-agent
示例用户代理我的计算机发送的信息是:
我认为通过服务器提供该信息非常有可能很好地猜测我的屏幕分辨率(DPI 等)。当然,您需要一个设备信息表来参考。
对于移动设备来说,这变得更加容易。如果用户代理是 iPad 的移动浏览器:
您可以非常确定地知道服务器上的屏幕分辨率是多少。您甚至可以通过该信息了解浏览器的高度和宽度。我怀疑许多移动设备(例如 Android 或 Win Mo)也是如此。
总而言之,我同意这样做不切实际,但我也不同意。
我相信 Facebook 已经开展了一个主要项目,对设备和屏幕分辨率进行分类,并将其开源,因为他们在创建 Facebook 移动应用程序时遇到了类似的问题,例如,他们必须对所有移动浏览器渲染器之间的所有差异进行分类,以便他们可以根据需要定制客户端应用程序每个个案。理论上,也许该项目可能拥有执行此操作所需的信息。
I don't agree entirely with the above correct answer. Realistically this answer is correct in many cases...but theoretically it is not. Often requests made to a web server contain a User-Agent field which could, in theory, be used to discern information about device screen resolutions and properties.
Web requests do not pass through the client first. They pass to the server, which then serves a page to the client so the server gets the request first...Ruby on Rails, for example, receives a request through an action controller to a resource and then serves a page to the response.
Look at an example UA parser such as : https://github.com/visionmedia/user-agent
A sample user agent being sent by my computer is:
I think it is eminently possible to make a good guess what my screen resolution (DPI etc) is given that information via a server. You would, of course, need a table of device information to reference.
For mobile devices it gets even easier. If the User-Agent is a mobile safari for iPad:
You can know with strong certainty what the screen resolution is on the server. You can even know the height and width of the browser with that info. I suspect the same is true with many mobile devices such as Android or Win Mo.
So in summation, I agree its impractical to do this but I also disagree.
I believe Facebook has undertaken a major project cataloging devices and screen resolutions and made it open source because they faced similar issues when creating the facebook mobile app e.g. they had to catalog all the discrepancies between all mobile browser renderers so they could tailor the client app to every individual case. Perhaps that project might have the necessary information to do this...in theory.
Ruby 运行在服务器端——没有从客户端获取信息,它无法了解任何客户端功能。
Ruby runs on the server side--without getting info from the client, it has no way of knowing any client capabilities.
对于不可能的事情,Mobvious 看起来做得不错:
https://github.com/jistr/mobvious
For something that's impossible it looks like Mobvious does a decent job:
https://github.com/jistr/mobvious
我遇到了同样的问题,并使用窗口高度的 getter 和 setter 路线解决了它。如果 $height 变量为 0,则提供 get_heigt.erb,否则提供 index.erb
这是一个单用户应用程序,因此我使用一个全局变量,对于不同的用户,您必须将该信息保留在 cookie 中。
这里是重要的代码。
控制器:
get_height.erb
I had the same problem and solved it using a getter and a setter route for the window height. If the $height variabale is 0 the get_heigt.erb is served, otherwise the index.erb
This is a one user app, so I use a global variable, with different users you would have to keep that info in cookies.
Here the code that matters.
Controller:
get_height.erb
你可以使用 Ahoy。 current_visit 方法包含以下信息。
You could use Ahoy. The current_visit method contains the following information.
不确定构建有多现实,但您可以构建一个数据库,该数据库将您可以从服务器端的用户代理获取的特定设备类型映射到已知的屏幕尺寸。它仍然不允许诸如窗口大小之类的东西。
Not sure how realistic would be to build but you could build a database which maps specific device types you can get from user agent in server-side into know screen sizes. It still would not allow things like window size.