Adaptive images for ruby based servers

假装没事ソ 提交于 2019-12-06 10:38:00

Media queries would only work with CSS background images.

By the way Adaptive-Images like solution aren't that difficult to implement.

They just serves images dynamically in place of the web server, caching adapted images to speed the process up and avoiding to waste resources.

Images are resized and cached depending on the value of a cookie that holds the requesting device resolution.

Saying that you're in Rails, take the following steps:

  1. Append this javascript line to <head> section in your layout:

    <script>document.cookie='resolution='+Math.max(screen.width,screen.height)+'; path=/‘;</script>

  2. add an AdaptiveImagesController with an action adapt_and_serve.

  3. Lets adapt_and_serve read/cache/adapt and serve (with a far-in-the-future exipiration header) the file corresponding to every request.

  4. Add a rule to routes.rb to catch image requests that need to be adapted and route them to AdaptiveImagesController#adapt_and_serve

I think there is nothing special in serving adaptive images . It depends on how you have set up your frontend: you use media queries to resolve the user's screen dimensions and decide the image size . You can use Rails framework for your purpose for sure . There are a number of responsive design implementations working fine with Rails - Twitter Bootstrap is a famous one .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!