问题
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).
What I'm looking to do, given a URL, is:
Take a screenshot of the website
Crop it (only take the top left most 100x100 pixels
PS. Here is my environment: Rails 3.1, Ruby 1.9.2
Note: The solution would probably need to follow any redirections on the URL as well.
Updates:
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.
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
The cropping will be easy with http://rmagick.rubyforge.org/ or https://github.com/thoughtbot/paperclip
回答1:
There is a Rails gem for this task.
gem install selenium-webdriver
Simple use case:
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
回答2:
This might help: https://github.com/csquared/IMGKit
回答3:
You could use wkhtmltoimage to load up the webpage and save it as an image, then imagemagick, (or one of the ruby wrappers for it) to crop it.
wkhtmltoimage www.google.com output.jpg
convert -crop 100x100+0+100 output.jpg cropped.jpg
There isn't a prebuilt wkhtmltoimage binary for OSX though, so perhaps you may want to use wkhtmltopdf instead and then imagemagick to convert to an image.
wkthmltopdf www.google.com output.pdf
convert -crop 100x100+0+100 output.pdf cropped.jpg
回答4:
A simple, but Mac only, solution seems to be http://www.paulhammond.org/webkit2png/
Just chmod -x
that script and use as python webkit2png http://www.google.com/
and it creates 3 files:
- Full screenshot
- Thumbnail of the top most portion of site
- Thumbnail of the full screenshot
来源:https://stackoverflow.com/questions/8218501/how-to-take-screenshot-of-a-website-with-rails-3-1-without-using-a-service