如何使用Rails 3.1截取网站的屏幕截图? -不使用服务 [英] How to take screenshot of a website with Rails 3.1? -- without using a service
问题描述
几乎每个答案我都使用一些现有服务找到了参考. 是否可以通过编程方式使用Rails 3.1做到这一点?使用PHP很难做到这一点(PHP中已有预构建的库可以做到这一点).
Almost every answer I've found references using some existing service. Is there a way to do this using Rails 3.1 programmatically? This was dead easy to do with PHP (there are prebuilt libraries in PHP that do this).
在给定URL的情况下,我要做的是:
What I'm looking to do, given a URL, is:
-
为网站截图
Take a screenshot of the website
裁剪(仅左上角最多100x100像素
Crop it (only take the top left most 100x100 pixels
PS.这是我的环境:Rails 3.1 ,Ruby 1.9.2
PS. Here is my environment: Rails 3.1, Ruby 1.9.2
注意:该解决方案可能还需要对URL进行任何重定向.
Note: The solution would probably need to follow any redirections on the URL as well.
更新:
-
我见过 https://github.com/topfunky/osxscreenshot 问题是它需要一个较旧版本的Ruby(1.8.x),只能在我的dev Mac机器上使用.
I've seen https://github.com/topfunky/osxscreenshot The problem is that it requires an older version of Ruby (1.8.x) and will only work on my dev Mac machine.
我已经看到了Wordpress的漏洞(他们提供的服务不会对用户可能滥用的任何速率进行限制).我不想滥用他们的资源以谋取利益... http://s.wordpress.com/mshots/v1/http%3A%2F%2Fstackoverflow.com%2F?w=500
I've seen the vulnerability with Wordpress (they have a service that doesn't do any rate limiting which someone could potentially abuse). I would not want to abuse their resources for my benefit... http://s.wordpress.com/mshots/v1/http%3A%2F%2Fstackoverflow.com%2F?w=500
使用 http://rmagick.rubyforge.org/或推荐答案
此任务有一个Rails瑰宝. There is a Rails gem for this task. 简单用例: 这篇关于如何使用Rails 3.1截取网站的屏幕截图? -不使用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! gem install selenium-webdriver
require 'selenium-webdriver'
width = 1024
height = 728
driver = Selenium::WebDriver.for :firefox
driver.navigate.to 'http://domain.com'
driver.execute_script %Q{
window.resizeTo(#{width}, #{height});
}
driver.save_screenshot('/tmp/screenshot.png')
driver.quit