简体   繁体   中英

How to make Paperclip crop and NOT scale an attached image?

I want Paperclip to crop, and not scale (see :full1 in this excerpt)

class Graphic < ActiveRecord::Base
  has_attached_file :image, :styles => { :full0 => "940x1000#" #want it to scale, and crop if neccessary
                                         :full1 => "940#", #want it to crop width, not scale
                                       }

I want :full1 to work, but it doesn't. By "work" I mean it should crop the image's width, but do nothing to it's height. The reason is I'm uploading web screenshots, and I want them to be trimmed to 940px wide (from the center), but their height should remain intact. As far as what I research on paperclip I'm not finding how to do this.

Apparently it's quite supported by ImageMagick: http://www.imagemagick.org/Usage/crop/#crop_strip But I don't know how to jam this into paperclip on rails.

Many thanks!

Could you just set the height to something absurdly large so that it will be a non-issue?

class Graphic < ActiveRecord::Base
  has_attached_file :image, :styles => { :full0 => "940x1000#" #want it to scale, and crop if neccessary
                                         :full1 => "940x9999999#", #want it to crop width, not scale
                                       }

I think that will crop anything wider than 940px.

You can user convert options for image process, Following will crop the image centrally.

has_attached_file :profile_picture, :storage => :s3,                             
                                     :styles => { :medium => "", :thumb => ""},
                                      :convert_options => {
                                          :thumb => Proc.new { |instance| instance.thumnail_dimension },
                                          :medium => Proc.new { |instance| instance.thumnail_dimension(300) }
                                          }

def thumnail_dimension(size=100)
    dimensions = Paperclip::Geometry.from_file(profile_picture.queued_for_write[:original].path)
    min = dimensions.width > dimensions.height ? dimensions.height : dimensions.width
    "-gravity Center -crop #{min}x#{min}+0+0 +repage -resize #{size}x#{size}^"
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM