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
965 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-20 18:22:40 -05:00
if !options.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] ||= ""
2009-10-28 17:54:25 -04:00
http_prefix = options.http_images_path rescue options.images_dir
2009-11-22 18:37:11 -05:00
2009-10-26 18:07:21 -04:00
begin
2009-10-28 17:54:25 -04:00
real_path = File.join(options.public, options.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