Module: Middleman::Extensions::AutomaticImageSizes::InstanceMethods

Defined in:
lib/middleman-more/extensions/automatic_image_sizes.rb

Overview

Automatic Image Sizes Instance Methods

Instance Method Summary collapse

Instance Method Details

#image_tag(path, params = {}) ⇒ String

Override default image_tag helper to automatically calculate and include image dimensions.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

Returns:

  • (String)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/middleman-more/extensions/automatic_image_sizes.rb', line 32

def image_tag(path, params={})
  if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
    params[:alt] ||= ""

    real_path = path
    real_path = File.join(images_dir, real_path) unless real_path =~ %r{^/}
    full_path = File.join(source_dir, real_path)

    if File.exists? full_path
      begin
        width, height = ::FastImage.size(full_path, :raise_on_failure => true)
        params[:width]  = width
        params[:height] = height
      rescue
        warn "Couldn't determine dimensions for image #{path}: #{$!.message}"
      end
    end
  end

  super(path, params)
end