class Middleman::Extensions::AutomaticImageSizes

Automatic Image Sizes extension

Public Class Methods

new(app, options_hash={}, &block) click to toggle source
Calls superclass method Middleman::Extension::new
# File lib/middleman-more/extensions/automatic_image_sizes.rb, line 3
def initialize(app, options_hash={}, &block)
  super

  # Include 3rd-party fastimage library
  require 'vendored-middleman-deps/fastimage'
end

Public Instance Methods

image_tag(path, params={}) click to toggle source

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

@param [String] path @param [Hash] params @return [String]

Calls superclass method
# File lib/middleman-more/extensions/automatic_image_sizes.rb, line 17
def image_tag(path, params={})
  if !params.key?(:width) && !params.key?(:height) && !path.include?('://')
    params[:alt] ||= ''

    real_path = path
    real_path = File.join(images_dir, real_path) unless real_path.start_with?('/')
    full_path = File.join(source_dir, real_path)

    if File.exist?(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}: #{$ERROR_INFO.message}"
      end
    end
  end

  super(path, params)
end