Sha256: cd67d53c95642c5843c81ea12f1d085f17a02d70cfb55b91dce2cc75dbddc9aa

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

module RailsUploads
  module Magick
    class Image

      def initialize(source, output=nil)
        @source = source
        @output = output
      end

      def convert(args)
        tokens = [convert? ? 'convert' : 'mogrify']
        file_to_tokens tokens
        args_to_tokens args, tokens
        tokens << "\"#{@output}\"" if convert?
        success, output = run(tokens)
        if block_given? 
          yield success, output
        else
          success
        end
      end

      def identify(args)
        tokens = ['identify']
        args_to_tokens args, tokens
        file_to_tokens tokens
        success, output = run(tokens)
        if block_given?
          yield success, output
        else
          success
        end
      end

      def dimensions
        identify(format: '%wx%h') do |success, output|
          success ? output.chomp.split('x').map(&:to_i) : []
        end
      end

      def width
        dimensions[0]
      end

      def height
        dimensions[1]
      end

      def resize_to_fill(max_width, max_height)
        width, height = dimensions
        scale = [max_width/width.to_f, max_height/height.to_f].max
        convert resize: "#{(scale*width).ceil}x#{(scale*height).ceil}", gravity: 'center', crop: "#{max_width}x#{max_height}+0+0" 
      end

      def resize_to_fit(max_width, max_height)
        width, height = dimensions
        scale = [max_width/width.to_f, max_height/height.to_f].min
        convert resize: "#{(scale*width).to_i}x#{(scale*height).to_i}", gravity: 'center'
      end

      protected

      def convert?
        not (@output.nil? and ::File.exists?(@output))
      end
  
      def file_to_tokens(tokens)
        tokens << (convert? ? "\"#{@source}\"" : @output)
      end
    
      def args_to_tokens(args, tokens)
        args.each { |k, v| tokens << "-#{k} #{v}" }
      end

      def run(tokens)
        output = `#{tokens.join(' ')}`
        success = $?.exitstatus == 0 
        [success, output]
      end 

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_uploads-0.2.8 lib/rails_uploads/magick/image.rb
rails_uploads-0.2.7 lib/rails_uploads/magick/image.rb
rails_uploads-0.2.6 lib/rails_uploads/magick/image.rb