7

I upload a photo, it is a rectangle. How Can I get it resized and filled to a square ?

I mean when the photo is horizontal positioned it should have above and under it, two white fields (for keeping the shape of a square) and when it is vertically, it should have two white fields on the sides of the photo.

When I used PHP, a have used this http://www.verot.net/php_class_upload_samples.htm

Have a look at the

100x150, keeping ratio, filling top and bottom

example

I'm using Paperclip with RoR. How is the best way to do that ?

astropanic
  • 10,800
  • 19
  • 72
  • 132

1 Answers1

17

Here's what I used on a rails 3 app w/ paperclip. I used the following ImageMagick options to make it centered: background, compose, gravity and extent. I'm using the mini_magick processor.

has_attached_file :image,
  :styles => { :large => ["855x570>", :jpg], :medium => ["432x288>", :jpg], :small => ["276x184>", :jpg], :tiny => ["195x130>", :jpg] },
  :processor => "mini_magick",
  :convert_options => {
    :medium => "-background white -compose Copy -gravity center -extent 432x288",
    :small => "-background white -compose Copy -gravity center -extent 276x184",
    :tiny => "-background white -compose Copy -gravity center -extent 195x130"
  }
danlee
  • 733
  • 8
  • 13
  • 1
    Well-written example. This works in Rails 4 with Paperclip 3.5.4. FYI Imagemagick defaults the background color to white so it is not necessary to specify that option. `medium: '-compose Copy -gravity center -extent 432x288'` – scarver2 Sep 04 '14 at 20:28
  • Just a head's up: make sure you don't have a trailing `#` in the style definitions. So, `:large = ["855x570#", :jpg]` will not work, since the `#` after the `855x570` tells paperclip to crop and center the image. – Richard Jones Mar 28 '17 at 17:05