Sha256: 256db17da31b2d3f901bf356650e721a16f82f1442bde93ac4010f7f9bab0e3b

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

# Automatic Image Sizes extension
class Middleman::Extensions::AutomaticImageSizes < ::Middleman::Extension
  def initialize(app, options_hash={}, &block)
    super

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

  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={})
      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
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
middleman-core-3.3.5 lib/middleman-more/extensions/automatic_image_sizes.rb
middleman-core-cj-3.3.6 lib/middleman-more/extensions/automatic_image_sizes.rb
middleman-core-cj-3.3.5 lib/middleman-more/extensions/automatic_image_sizes.rb
middleman-core-cj-3.3.4 lib/middleman-more/extensions/automatic_image_sizes.rb
middleman-core-3.3.4 lib/middleman-more/extensions/automatic_image_sizes.rb
middleman-core-3.3.3 lib/middleman-more/extensions/automatic_image_sizes.rb