Skip to content

Instantly share code, notes, and snippets.

@todesking
Last active December 10, 2015 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todesking/4351273 to your computer and use it in GitHub Desktop.
Save todesking/4351273 to your computer and use it in GitHub Desktop.
gem "fastimage", "~>1.2.13"
module ApplicationHelper
include ActionView::Helpers::AssetTagHelper
# 画像サイズ見て自動でwidth/height属性つけるimage_tag
def image_tag_with_auto_size(source, options)
if options[:size] || options[:width] || options[:height]
image_tag_without_auto_size(source, options)
else
begin
width, height = FastImage.size(File.join(Rails.root, 'public', source))
if request.smart_phone?
width /= 2
height /= 2
end
image_tag_without_auto_size(source, options.merge(width: width, height: height))
rescue FastImage::FastImageException => e
Rails.logger.error("FastImage.size failed: #{e.inspect}")
image_tag_without_auto_size(source, options)
end
end
end
alias_method_chain :image_tag, :auto_size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment