Sha256: 16fcf51292906da596c481a4a831863093a982a356baaf8344e53b54bfe600ad

Contents?: true

Size: 1.07 KB

Versions: 13

Compression:

Stored size: 1.07 KB

Contents

module Locomotive
  module Steam

    class ImageResizerService

      attr_accessor_initialize :resizer, :asset_path

      def resize(source, geometry)
        return nil if disabled? || geometry.blank?

        if file = fetch_file(source)
          file.thumb(geometry).url
        else
          Locomotive::Common::Logger.error "Unable to resize on the fly: #{source.inspect}"
          nil
        end
      end

      def disabled?
        resizer.nil? || resizer.plugins[:imagemagick].nil?
      end

      protected

      def fetch_file(source)
        return nil if source.blank?

        url_or_path = get_url_or_path(source)

        if url_or_path =~ Steam::IsHTTP
          resizer.fetch_url(url_or_path)
        else
          path = url_or_path.sub(/(\?.*)$/, '')
          resizer.fetch_file(File.join(asset_path || '', path))
        end
      end

      def get_url_or_path(source)
        if source.is_a?(Hash)
          source['url']
        elsif source.respond_to?(:url)
          source.url
        else
          source
        end.strip
      end

    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
locomotivecms_steam-1.1.2 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.2.0.beta1 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.1.1 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.1.0 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.1.0.rc3 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.1.0.rc2 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.1.0.rc1 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.0.1 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.0.0 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.0.0.rc10 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.0.0.rc9 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.0.0.rc8 lib/locomotive/steam/services/image_resizer_service.rb
locomotivecms_steam-1.0.0.rc6 lib/locomotive/steam/services/image_resizer_service.rb