如何在没有第三方工具的情况下生成网站缩略图?

是否有任何产生网站缩略图的Rails 3.2.1的gem? 我看到很多第三方解决方案,但我不喜欢它们不在我的服务器上托管的事实。 我正在构建的应用程序尽可能稳定非常重要,我认为从长远来看这不是一个好的解决方案。

我的ruby知识相当不错,我认为足够使用gem并实现它,但如果没有gem存在,绝对不能从头开始写这样的东西。

谢谢!

你可以试试蜻蜓或载波

好吧,这是Rubygems上出现的第一件事: thumbnailer 。 它使用亚马逊并且每生成一张图片需要支付少量费用,所以你可能不希望这样……

但是还有thumbnailer-ruby ,看起来它完全适用于本地机器。 但是没有测试过。 看起来这实际上并不是你想要的。 没关系。

现在另一个名为snapurl的gem看起来很漂亮。 再一次,我还没有尝试过。 我现在就这样做。

编辑:不会为我竞选; 因错误而一直失败。

一个Browshot有一个gem可用 。

没有必要为此使用第三方服务。 您可以在模型中执行以下操作:

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 

原帖是在这个博客上: http : //sourcey.com