How do I generate website thumbnails without 3rd party tools?

依然范特西╮ 提交于 2019-12-07 05:49:34

问题


Are there any gems out there for Rails 3.2.1 that generate website thumbnails? I see a lot of 3rd party solutions but I don't like the fact that they aren't hosted on my server. It's really important the app I'm building is as stable as possible and I think this is not a good solution in the long run.

My ruby knowledge is fairly good, I think enough to use a gem and implement it, but definitely not good enough to write something like this from scratch if no gems exist.

Thanks!


回答1:


You could try dragonfly or carrierwave




回答2:


Well, here's the first thing that came up on Rubygems: thumbnailer. It uses Amazon and costs a small fee per image it generates, so you probably don't want this...

But there's also thumbnailer-ruby which looks like it works completely on the local machine. Haven't tested it out, though. It appears that this doesn't actually do what you want. Nevermind.

Now another gem called snapurl looks pretty fancy. Once again, I haven't tried it out yet. I'll do that now.

EDIT: Won't run for me; keeps failing with an error.




回答3:


https://url2png.com/ has worked great so far




回答4:


aBrowshot has a gem available.




回答5:


There's no need to use a third party service for this. You can do something like this in your model:

class MySexyModel < ActiveRecord::Base

  ... stuff

  # Generate the thumbnail on validate so we can return errors on failure
  validate :generate_thumbnail_from_url

  # Cleanup temp files when we are done
  after_save :cleanup_temp_thumbnail

  # Generate a thumbnail from the remote URL
  def generate_thumbnail_from_url

    # Skip thumbnail generation if:
    # a) there are already other validation errors
    # b) an image was manually specified
    # c) an image is already stored and the URL hasn't changed
    skip_generate = self.errors.any? || (self.image_changed? ||
        (self.image_stored? && !self.url_changed?))
    # p "*** generating thumbnail: #{!skip_generate}"
    return if skip_generate

    # Generate and assign an image or set a validation error
    begin
      tempfile = temp_thumbnail_path
      cmd = "wkhtmltoimage --quality 95 \"#{self.url}\" \"#{tempfile}\""
      # p "*** grabbing thumbnail: #{cmd}"
      system(cmd) # sometimes returns false even if image was saved
      self.image = File.new(tempfile) # will throw if not saved
    rescue => e
      # p "*** thumbnail error: #{e}"
      self.errors.add(:base, "Cannot generate thumbnail. Is your URL valid?")
    ensure
    end
  end

  # Return the absolute path to the temporary thumbnail file
  def temp_thumbnail_path
    File.expand_path("#{self.url.parameterize.slice(0, 20)}.jpg", Dragonfly.app.datastore.root_path)
  end

  # Cleanup the temporary thumbnail image
  def cleanup_temp_thumbnail
    File.delete(temp_thumbnail_path) rescue 0
  end
end

The original post is on this blog: http://sourcey.com



来源:https://stackoverflow.com/questions/9449065/how-do-i-generate-website-thumbnails-without-3rd-party-tools

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