How to crop image on upload with Rails, Carrierwave and Minimagick?

后端 未结 2 1990
北恋
北恋 2021-02-04 19:35

I have read:

  • Undefined Method crop! Using Carrierwave with MiniMagick on rails 3.1.3
  • Carrierwave Cropping
  • http://pastebin.com/ue4mVbC8
相关标签:
2条回答
  • 2021-02-04 19:58

    In #cropper your crop_h is not initialized(you double initialize crop_y instead). In #crop error is exactly what error message said - you don't have defined crop_x for Project class.

    0 讨论(0)
  • 2021-02-04 20:10

    I have understood.

      version :thumb do    
        process resize_to_fit: [300, nil]
        process crop: '300x150+0+0'
        #process resize_and_crop: 200
      end
    
    private
    
      # Simplest way
      def crop(geometry)
        manipulate! do |img|      
          img.crop(geometry)
          img
        end    
      end
    
      # Resize and crop square from Center
      def resize_and_crop(size)  
        manipulate! do |image|                 
          if image[:width] < image[:height]
            remove = ((image[:height] - image[:width])/2).round 
            image.shave("0x#{remove}") 
          elsif image[:width] > image[:height] 
            remove = ((image[:width] - image[:height])/2).round
            image.shave("#{remove}x0")
          end
          image.resize("#{size}x#{size}")
          image
        end
      end
    

    resize_and_crop from here:

    http://blog.aclarke.eu/crop-and-resize-an-image-using-minimagick/

    0 讨论(0)
提交回复
热议问题