How to respond_to PNG or JPG in Rails and generate image from HTML?

纵饮孤独 提交于 2019-12-02 19:39:21

Check out here: http://weblog.rubyonrails.org/2006/12/19/using-custom-mime-types

Mime::Type.register "image/png", :png

# then in your controller action
def show
  respond_to do |format|
    format.html { }
    format.png { }
  end
end

UPD

What about image generating. If you need to convert your HTML page into image. You can use wkhtmltoimage
http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltoimage-0.10.0_beta2-static-amd64.tar.bz2&can=4&q=

There is no gem like pdfkit for wkhtmltopdf but it is easy to use.

Also you can use pdfKIT gem and after that convert PDF to PNG with imagemagick. That is quite easy too.

UPD

Istead of using SnapShot I prefer to use IMGKit gem

https://github.com/csquared/IMGKit

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