2010-09-04 23:26:48 -04:00
|
|
|
class Middleman::Features::AutomaticImageSizes
|
2010-09-05 22:28:38 -04:00
|
|
|
def initialize(app, config)
|
2010-09-04 23:26:48 -04:00
|
|
|
require "middleman/features/automatic_image_sizes/fastimage"
|
2009-10-26 18:07:21 -04:00
|
|
|
|
2010-09-05 22:28:38 -04:00
|
|
|
Middleman::Server.helpers do
|
2010-09-05 22:33:57 -04:00
|
|
|
alias_method :pre_automatic_image_tag, :image_tag
|
2010-09-04 23:26:48 -04:00
|
|
|
def image_tag(path, params={})
|
|
|
|
if (!params[:width] || !params[:height]) && !path.include?("://")
|
|
|
|
params[:alt] ||= ""
|
|
|
|
http_prefix = settings.http_images_path rescue settings.images_dir
|
2009-11-22 18:37:11 -05:00
|
|
|
|
2010-09-04 23:26:48 -04:00
|
|
|
begin
|
|
|
|
real_path = File.join(settings.public, settings.images_dir, path)
|
|
|
|
if File.exists? real_path
|
2010-09-05 22:33:57 -04:00
|
|
|
dimensions = ::FastImage.size(real_path, :raise_on_failure => true)
|
2010-09-04 23:26:48 -04:00
|
|
|
params[:width] ||= dimensions[0]
|
|
|
|
params[:height] ||= dimensions[1]
|
|
|
|
end
|
|
|
|
rescue
|
2009-10-26 18:07:21 -04:00
|
|
|
end
|
2009-10-26 18:08:21 -04:00
|
|
|
|
2010-09-04 23:26:48 -04:00
|
|
|
capture_haml { haml_tag(:img, params.merge(:src => asset_url(path, http_prefix))) }
|
|
|
|
else
|
|
|
|
pre_automatic_image_tag(path, params)
|
|
|
|
end
|
2009-10-26 18:07:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-09-04 23:26:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Middleman::Features.register :automatic_image_sizes, Middleman::Features::AutomaticImageSizes
|