Sha256: 0f659a005e32541b950569b8a562da1076c9060f25df4f71de67c7daab67d8d1

Contents?: true

Size: 1.53 KB

Versions: 36

Compression:

Stored size: 1.53 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 '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?('://')
        real_path = path.dup
        real_path = File.join(config[:images_dir], real_path) unless real_path.start_with?('/')

        file = app.files.find(:source, real_path) || app.files.find(:source, real_path.sub(/^\//, ''))

        if file && file[:full_path].exist?
          begin
            full_path = file[:full_path].to_s
            width, height = ::FastImage.size(full_path, raise_on_failure: true)
            # Check for @2x and @3x image
            retina = full_path.match(/@(\d)x\.[a-zA-Z]{3,4}$/)
            if retina
              factor = retina[1].to_i
              width /= factor
              height /= factor
            end
            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

36 entries across 36 versions & 3 rubygems

Version Path
middleman-core-4.5.1 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.5.0 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.4.3 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.4.2 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.4.0 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.11 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.10 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.8 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.7 lib/middleman-core/extensions/automatic_image_sizes.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/middleman-core-4.3.3/lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.6 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.5 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.4 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.3 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.2 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.1 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.0 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-with-external-sources-watch-fix-4.1.10 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-with-external-sources-watch-fix-4.1.0 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-4.3.0.rc.1 lib/middleman-core/extensions/automatic_image_sizes.rb