2013-04-20 23:27:25 +02:00
|
|
|
# Automatic Image Sizes extension
|
|
|
|
class Middleman::Extensions::AutomaticImageSizes < ::Middleman::Extension
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
2013-04-20 22:32:39 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Include 3rd-party fastimage library
|
2013-12-28 01:26:31 +01:00
|
|
|
require 'vendored-middleman-deps/fastimage'
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2013-04-20 22:32:39 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
helpers do
|
|
|
|
# Override default image_tag helper to automatically calculate and include
|
|
|
|
# image dimensions.
|
|
|
|
#
|
|
|
|
# @param [String] path
|
|
|
|
# @param [Hash] params
|
|
|
|
# @return [String]
|
|
|
|
def image_tag(path, params={})
|
2013-12-28 01:26:31 +01:00
|
|
|
if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?('://')
|
|
|
|
params[:alt] ||= ''
|
2013-04-20 22:32:39 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
real_path = path
|
2014-01-01 03:21:30 +01:00
|
|
|
real_path = File.join(config[:images_dir], real_path) unless real_path.start_with?('/')
|
2013-04-20 23:27:25 +02:00
|
|
|
full_path = File.join(source_dir, real_path)
|
2013-04-20 22:32:39 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
if File.exists?(full_path)
|
|
|
|
begin
|
|
|
|
width, height = ::FastImage.size(full_path, :raise_on_failure => true)
|
|
|
|
params[:width] = width
|
|
|
|
params[:height] = height
|
|
|
|
rescue FastImage::UnknownImageType
|
|
|
|
# No message, it's just not supported
|
|
|
|
rescue
|
|
|
|
warn "Couldn't determine dimensions for image #{path}: #{$!.message}"
|
2013-04-20 22:32:39 +02:00
|
|
|
end
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
|
|
|
|
super(path, params)
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
|
|
|
end
|
2012-07-09 09:16:54 +02:00
|
|
|
end
|