<commit_before>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).to_i}x#{(scale*height).to_i}", :gravity => 'center', :crop => "#{max_width}x#{max_height}+0+0" 
      end

      def resize_to_fit(max_width, max_height)
        width, height = dimensions
        ratios = [max_width/width.to_f, max_height/height.to_f]
        scale = ratios.send((max_width < width or max_height < height) ? :max : :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
  <commit_msg>Fix fit resize method<commit_after>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).to_i}x#{(scale*height).to_i}", :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
  
{'fraction_non_alphanumeric': 0.1130088243666382, 'fraction_numerical': 0.002277255906632508, 'mean_word_length': 2.0059880239520957, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6324048', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1046, 'n_words': 253}