1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/lib/middleman/features/automatic_image_sizes.rb

31 lines
966 B
Ruby
Raw Normal View History

2009-10-26 18:07:21 -04:00
require "middleman/fastimage"
class Middleman::Base
alias_method :pre_automatic_image_tag, :image_tag
helpers do
2009-10-28 17:54:25 -04:00
def image_tag(path, params={})
2010-01-30 01:48:34 -05:00
if !self.enabled?(:automatic_image_sizes)
2009-11-22 18:37:11 -05:00
return pre_automatic_image_tag(path, params)
end
2009-10-26 18:07:21 -04:00
if (!params[:width] || !params[:height]) && !path.include?("://")
2009-10-26 18:08:21 -04:00
params[:alt] ||= ""
2010-01-30 01:48:34 -05:00
http_prefix = settings.http_images_path rescue settings.images_dir
2009-11-22 18:37:11 -05:00
2009-10-26 18:07:21 -04:00
begin
2010-01-30 01:48:34 -05:00
real_path = File.join(settings.public, settings.images_dir, path)
2009-10-26 18:07:21 -04:00
if File.exists? real_path
dimensions = Middleman::FastImage.size(real_path, :raise_on_failure => true)
params[:width] ||= dimensions[0]
params[:height] ||= dimensions[1]
end
rescue
end
2009-10-26 18:08:21 -04:00
2009-11-22 18:37:11 -05:00
capture_haml { haml_tag(:img, params.merge(:src => asset_url(path, http_prefix))) }
2009-10-26 18:08:21 -04:00
else
pre_automatic_image_tag(path, params)
2009-10-26 18:07:21 -04:00
end
end
end
end