开发者

How to I resize images with the auto_html gem in ruby on rails?

开发者 https://www.devze.com 2023-01-08 14:43 出处:网络
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?

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消