Search code examples
ruby-on-railsruby-on-rails-3imagemagickminimagick

Image Resize and Crop Algorithm In MiniMagick


I'm running a rails app that accepts user screenshots. We are looking to generate smaller versions of the screenshots in a very specific dimension (652x408. Why that? ask the front end guy.) We are using Carrierwave and Minimagick currently to accomplish this.

I've tried the resize_to_fill and resize_to_fit resize methods, but they always leave some whitespace with inputs. I'm trying to resort to my own custom one, but I'm pretty lost on the imagemagick syntax.

Anyway, I'm trying to implement this, but I have no idea the best way to go about it. Is there a standard algorithm for this kind of thing?


Solution

  • You can use Jcrop and Carrierwave to crop image. Like this effect

    enter image description here

    Here is some code https://github.com/luckyyang/imagecrop But it's use Paperclip instand of Carrierwave.

    In carriewave, there is some code

      version :thumb do
        process :crop
        resize_to_fill(240, 320)
      end
    
      # crop image
      def crop
        if model.crop_x.present?
        resize_to_limit(1280, 1280)
        manipulate! do |img|
          x = model.crop_x
          y = model.crop_y
          w = model.crop_w
          h = model.crop_h
          img.crop("#{w}x#{h}+#{x}+#{y}")
          img
        end
      end
    end
    

    I feel apologetic with my bad English. I hope this can help you