tabaluga
tabaluga

Reputation: 1417

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

Upvotes: 0

Views: 399

Answers (1)

William
William

Reputation: 3529

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!

Upvotes: 2

Related Questions