Sha256: ea07af74d6e52d3e4ce7ee21bcd8503df13689aa6e3533cdd0912a6de032c79e

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

# Extensions namespace
module Middleman::Extensions
  
  # Automatic Image Sizes extension
  module AutomaticImageSizes
    
    # Setup extension
    class << self
      
      # Once registered
      def registered(app)
        # Include 3rd-party fastimage library
        require "middleman-core/extensions/automatic_image_sizes/fastimage"

        # Include methods
        app.send :include, InstanceMethods
      end
      
      alias :included :registered
    end
  
    # Automatic Image Sizes Instance Methods
    module InstanceMethods
      
      # 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.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
          params[:alt] ||= ""
          http_prefix = http_images_path rescue images_dir

          begin
            real_path = File.join(source, images_dir, path)
            full_path = File.expand_path(real_path, root)
            http_prefix = http_images_path rescue images_dir
            if File.exists? full_path
              dimensions = ::FastImage.size(full_path, :raise_on_failure => true)
              params[:width]  = dimensions[0]
              params[:height] = dimensions[1]
            end
          rescue
            # $stderr.puts params.inspect
          end
        end
      
        super(path, params)
      end
    end
  end
  
  # Register the extension
  register :automatic_image_sizes, AutomaticImageSizes
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
middleman-core-3.0.0.beta.3 lib/middleman-core/extensions/automatic_image_sizes.rb
middleman-core-3.0.0.beta.2 lib/middleman-core/extensions/automatic_image_sizes.rb