Last active
December 10, 2015 00:28
-
-
Save todesking/4351273 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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