How to I resize images with the auto_html gem in ruby on rails?
I am using the auto_html gem to embedd images and videos in my rails app. But I开发者_如何学编程 've got a small problem. Is there a way to resize the images to a predefined size?
Thanks in advance lg tabaluga
You could create a new filter for images, and add the filter in an initializer after requiring the auto_html gem. The filter might look as follows:
AutoHtml.add_filter(:sized_image).with(:width => INSERT_SOME_DEFAULT_HERE, :height => INSERT_SOME_DEFAULT_HERE) do |text, options|
text.gsub(/http:\/\/.+\.(jpg|jpeg|bmp|gif|png)(\?\S+)?/i) do |match|
width = options[:width]
height= options[:height]
%|<img src="#{match}" alt="" width="#{width}" height="#{height}" />|
end
end
This is untested, as I do not currently have terminal access, but should work or at least get you pointed in the right direction. Good luck!
精彩评论