<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -7,7 +7,7 @@ module Technoweenie # :nodoc:
           base.send :extend, ClassMethods
           base.alias_method_chain :process_attachment, :processing
         end
-
+ 
         module ClassMethods
           # Yields a block containing an MiniMagick Image for the given binary data.
           def with_image(file, &amp;block)
@@ -23,23 +23,29 @@ module Technoweenie # :nodoc:
             !binary_data.nil?
           end
         end
-
+ 
       protected
         def process_attachment_with_processing
           return unless process_attachment_without_processing
           with_image do |img|
             resize_image_or_thumbnail! img
-            self.width  = img[:width] if respond_to?(:width)
-            self.height = img[:height]  if respond_to?(:height)
+            self.width = img[:width] if respond_to?(:width)
+            self.height = img[:height] if respond_to?(:height)
             callback_with_args :after_resize, img
           end if image?
         end
-
+ 
         # Performs the actual resizing operation for a thumbnail
         def resize_image(img, size)
           size = size.first if size.is_a?(Array) &amp;&amp; size.length == 1
           img.combine_options do |commands|
             commands.strip unless attachment_options[:keep_profile]
+
+            # gif are not handled correct, this is a hack, but it seems to work.
+            if img.output =~ / GIF /
+              img.format(&quot;png&quot;)
+            end           
+            
             if size.is_a?(Fixnum) || (size.is_a?(Array) &amp;&amp; size.first.is_a?(Fixnum))
               if size.is_a?(Fixnum)
                 size = [size, size]
@@ -69,27 +75,8 @@ module Technoweenie # :nodoc:
               
               # only crop if image is not smaller in both dimensions
               unless image_width &lt; thumb_width and image_height &lt; thumb_height
-              
-                # special cases, image smaller in one dimension then thumbsize
-                if image_width &lt; thumb_width
-                  offset = (image_height / 2) - (thumb_height / 2)
-                  command = &quot;#{image_width}x#{thumb_height}+0+#{offset}&quot;
-                elsif image_height &lt; thumb_height
-                  offset = (image_width / 2) - (thumb_width / 2)
-                  command = &quot;#{thumb_width}x#{image_height}+#{offset}+0&quot;
-                
-                # normal thumbnail generation
-                # calculate height and offset y, width is fixed                 
-                elsif (image_aspect &lt;= thumb_aspect or image_width &lt; thumb_width) and image_height &gt; thumb_height
-                  height = image_width / thumb_aspect
-                  offset = (image_height / 2) - (height / 2)
-                  command = &quot;#{image_width}x#{height}+0+#{offset}&quot;
-                # calculate width and offset x, height is fixed
-                else
-                  width = image_height * thumb_aspect
-                  offset = (image_width / 2) - (width / 2)
-                  command = &quot;#{width}x#{image_height}+#{offset}+0&quot;
-                end
+                command = calculate_offset(image_width,image_height,image_aspect,thumb_width,thumb_height,thumb_aspect)
+
                 # crop image
                 commands.extract(command)
               end
@@ -103,14 +90,43 @@ module Technoweenie # :nodoc:
               else
                 commands.resize(&quot;#{size.to_s}&quot;)
               end
-						# crop end
+            # crop end
             else
               commands.resize(size.to_s)
             end
           end
           temp_paths.unshift img
         end
+
+        def calculate_offset(image_width,image_height,image_aspect,thumb_width,thumb_height,thumb_aspect)
+        # only crop if image is not smaller in both dimensions
+
+          # special cases, image smaller in one dimension then thumbsize
+          if image_width &lt; thumb_width
+            offset = (image_height / 2) - (thumb_height / 2)
+            command = &quot;#{image_width}x#{thumb_height}+0+#{offset}&quot;
+          elsif image_height &lt; thumb_height
+            offset = (image_width / 2) - (thumb_width / 2)
+            command = &quot;#{thumb_width}x#{image_height}+#{offset}+0&quot;
+
+          # normal thumbnail generation
+          # calculate height and offset y, width is fixed                 
+          elsif (image_aspect &lt;= thumb_aspect or image_width &lt; thumb_width) and image_height &gt; thumb_height
+            height = image_width / thumb_aspect
+            offset = (image_height / 2) - (height / 2)
+            command = &quot;#{image_width}x#{height}+0+#{offset}&quot;
+          # calculate width and offset x, height is fixed
+          else
+            width = image_height * thumb_aspect
+            offset = (image_width / 2) - (width / 2)
+            command = &quot;#{width}x#{image_height}+#{offset}+0&quot;
+          end
+          # crop image
+          command
+        end
+
+
       end
     end
   end
-end
+end
\ No newline at end of file</diff>
      <filename>lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb</filename>
    </modified>
    <modified>
      <diff>@@ -35,7 +35,7 @@ end
 class ImageWithThumbsAttachment &lt; Attachment
   has_attachment :thumbnails =&gt; { :thumb =&gt; [50, 50], :geometry =&gt; 'x50' }, :resize_to =&gt; [55,55]
   after_resize do |record, img|
-    record.aspect_ratio = img.columns.to_f / img.rows.to_f
+   # record.aspect_ratio = img.columns.to_f / img.rows.to_f
   end
 end
 
@@ -53,7 +53,7 @@ class ImageWithThumbsFileAttachment &lt; FileAttachment
   has_attachment :path_prefix =&gt; 'vendor/plugins/attachment_fu/test/files',
     :thumbnails =&gt; { :thumb =&gt; [50, 50], :geometry =&gt; 'x50' }, :resize_to =&gt; [55,55]
   after_resize do |record, img|
-    record.aspect_ratio = img.columns.to_f / img.rows.to_f
+  #  record.aspect_ratio = img.columns.to_f / img.rows.to_f
   end
 end
 
@@ -130,9 +130,44 @@ begin
     has_attachment :path_prefix =&gt; 'vendor/plugins/attachment_fu/test/files',
       :processor =&gt; :mini_magick, :thumbnails =&gt; { :thumb =&gt; [50, 51], :geometry =&gt; '31&gt;' }, :resize_to =&gt; 55
   end
+  class ImageThumbnailCrop &lt; MiniMagickAttachment
+    has_attachment :path_prefix =&gt; 'vendor/plugins/attachment_fu/test/files',
+    :thumbnails =&gt; { :square =&gt; &quot;50x50c&quot;, :vertical =&gt; &quot;30x60c&quot;, :horizontal =&gt; &quot;60x30c&quot;}
+    
+    # TODO this is a bad duplication, this method is in the MiniMagick Processor
+    def self.calculate_offset(image_width,image_height,image_aspect,thumb_width,thumb_height,thumb_aspect)
+    # only crop if image is not smaller in both dimensions
+
+      # special cases, image smaller in one dimension then thumbsize
+      if image_width &lt; thumb_width
+        offset = (image_height / 2) - (thumb_height / 2)
+        command = &quot;#{image_width}x#{thumb_height}+0+#{offset}&quot;
+      elsif image_height &lt; thumb_height
+        offset = (image_width / 2) - (thumb_width / 2)
+        command = &quot;#{thumb_width}x#{image_height}+#{offset}+0&quot;
+
+      # normal thumbnail generation
+      # calculate height and offset y, width is fixed                 
+      elsif (image_aspect &lt;= thumb_aspect or image_width &lt; thumb_width) and image_height &gt; thumb_height
+        height = image_width / thumb_aspect
+        offset = (image_height / 2) - (height / 2)
+        command = &quot;#{image_width}x#{height}+0+#{offset}&quot;
+      # calculate width and offset x, height is fixed
+      else
+        width = image_height * thumb_aspect
+        offset = (image_width / 2) - (width / 2)
+        command = &quot;#{width}x#{image_height}+#{offset}+0&quot;
+      end
+      # crop image
+      command
+    end
+  end
+
 rescue MissingSourceFile
 end
 
+
+
 begin
   class S3Attachment &lt; ActiveRecord::Base
     has_attachment :storage =&gt; :s3, :processor =&gt; :rmagick, :s3_config_path =&gt; File.join(File.dirname(__FILE__), '../amazon_s3.yml')</diff>
      <filename>test/fixtures/attachment.rb</filename>
    </modified>
    <modified>
      <diff>@@ -23,9 +23,81 @@ class MiniMagickTest &lt; Test::Unit::TestCase
       assert_equal 31, geo.width
       assert_equal 40, geo.height
     end
+
+    def test_should_crop_image(klass = ImageThumbnailCrop)
+      attachment_model klass
+      attachment = upload_file :filename =&gt; '/files/rails.png'
+      assert_valid attachment
+      assert  attachment.image?
+    #  has_attachment :thumbnails =&gt; { :square =&gt; &quot;50x50c&quot;, :vertical =&gt; &quot;30x60c&quot;, :horizontal =&gt; &quot;60x30c&quot;}
+
+      square      = attachment.thumbnails.detect { |t| t.filename =~ /_square/ }
+      vertical    = attachment.thumbnails.detect { |t| t.filename =~ /_vertical/ }
+      horizontal  = attachment.thumbnails.detect { |t| t.filename =~ /_horizontal/ }
+      
+      # test excat resize
+      assert_equal 50, square.width
+      assert_equal 50, square.height
+
+      assert_equal 30, vertical.width
+      assert_equal 60, vertical.height
+
+      assert_equal 60, horizontal.width
+      assert_equal 30, horizontal.height
+    end
+    
+    # tests the first step in resize, crop the image in original size to right format
+    def test_should_crop_image_right(klass = ImageThumbnailCrop)      
+      @@testcases.collect do |testcase| 
+        image_width, image_height, thumb_width, thumb_height = testcase[:data]
+        image_aspect, thumb_aspect = image_width/image_height, thumb_width/thumb_height
+        crop_comand = klass.calculate_offset(image_width, image_height, image_aspect, thumb_width, thumb_height,thumb_aspect)
+        # pattern matching on crop command
+        if testcase.has_key?(:height) 
+          assert crop_comand.match(/^#{image_width}x#{testcase[:height]}\+0\+#{testcase[:yoffset]}$/)
+        else 
+          assert crop_comand.match(/^#{testcase[:width]}x#{image_height}\+#{testcase[:xoffset]}\+0$/)
+        end
+      end
+    end
+
   else
     def test_flunk
       puts &quot;MiniMagick not loaded, tests not running&quot;
     end
   end
+
+  @@testcases = [
+    # image_aspect &lt;= 1 &amp;&amp; thumb_aspect &gt;= 1  
+    {:data =&gt; [10.0,40.0,2.0,1.0], :height =&gt; 5.0, :yoffset =&gt; 17.5}, #   1b
+    {:data =&gt; [10.0,40.0,1.0,1.0], :height =&gt; 10.0, :yoffset =&gt; 15.0}, #  1b
+
+    # image_aspect &lt; 1 &amp;&amp; thumb_aspect &lt; 1
+    {:data =&gt; [10.0,40.0,1.0,2.0], :height =&gt; 20.0, :yoffset =&gt; 10.0}, # 1a
+    {:data =&gt; [2.0,3.0,1.0,2.0], :width =&gt; 1.5, :xoffset =&gt; 0.25}, # 1a
+
+    # image_aspect = thumb_aspect
+    {:data =&gt; [10.0,10.0,1.0,1.0], :height =&gt; 10.0, :yoffset =&gt; 0.0}, # QUADRAT 1c
+
+    # image_aspect &gt;= 1 &amp;&amp; thumb_aspect &gt; 1     &amp;&amp; image_aspect &lt; thumb_aspect
+    {:data =&gt; [6.0,3.0,4.0,1.0], :height =&gt; 1.5, :yoffset =&gt; 0.75}, # 2b  
+    {:data =&gt; [6.0,6.0,4.0,1.0], :height =&gt; 1.5, :yoffset =&gt; 2.25}, # 2b  
+
+    # image_aspect &gt; 1 &amp;&amp; thumb_aspect &gt; 1     &amp;&amp; image_aspect &gt; thumb_aspect
+    {:data =&gt; [9.0,3.0,2.0,1.0], :width =&gt; 6.0, :xoffset =&gt; 1.5}, # 2a
+
+    # image_aspect &gt; 1 &amp;&amp; thumb_aspect &lt; 1 &amp;&amp; image_aspect &lt; thumb_aspect
+    {:data =&gt; [10.0,5.0,0.1,2.0], :width =&gt; 0.25, :xoffset =&gt; 4.875}, # 4
+    {:data =&gt; [10.0,5.0,1.0,2.0], :width =&gt; 2.5, :xoffset =&gt; 3.75}, # 4
+
+    # image_aspect &gt; 1 &amp;&amp; thumb_aspect &gt; 1     &amp;&amp; image_aspect &gt; thumb_aspect
+    {:data =&gt; [9.0,3.0,2.0,1.0], :width =&gt; 6.0, :xoffset =&gt; 1.5}, # 3a    
+    # image_aspect &gt; 1 &amp;&amp; thumb_aspect &gt; 1     &amp;&amp; image_aspect &lt; thumb_aspect
+    {:data =&gt; [9.0,3.0,5.0,1.0], :height =&gt; 1.8, :yoffset =&gt; 0.6} # 3a
+  ]
+
+
+
+
+
 end</diff>
      <filename>test/processors/mini_magick_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>ee4ea169b4593b87ebcbd56fbaa91a8bc620300b</id>
    </parent>
  </parents>
  <author>
    <name>Tim Assmann</name>
    <email>tassmann@jovoto.com</email>
  </author>
  <url>http://github.com/richpoirier/attachment_fu/commit/01c80b7c842a82884054cc58de2f667a13737c73</url>
  <id>01c80b7c842a82884054cc58de2f667a13737c73</id>
  <committed-date>2008-10-22T03:55:49-07:00</committed-date>
  <authored-date>2008-10-22T03:55:49-07:00</authored-date>
  <message>* added workaround for better gif handling
* added basic test for new crop method
* added tests for croping the right part of various images with various attachment formats
  it needs a little help because of code duplication. How can i access a method in the processor to test if it works? I duplicated the method into the testclass in fixtures/attachment</message>
  <tree>ff4a798d6cadd525192d3606f9fecc6141aa6a42</tree>
  <committer>
    <name>Tim Assmann</name>
    <email>tassmann@jovoto.com</email>
  </committer>
</commit>
