【问题标题】:How to make Paperclip crop and NOT scale an attached image?如何使回形针裁剪而不缩放附加图像?
【发布时间】:2013-03-03 15:09:51
【问题描述】:

我希望 Paperclip 进行裁剪,而不是缩放(请参阅此摘录中的 :full1)

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
                                       }

我希望 :full1 工作,但它没有。通过“工作”,我的意思是它应该裁剪图像的宽度,但对它的高度不做任何事情。原因是我正在上传网络截图,我希望它们被修剪为 940px 宽(从中心),但它们的高度应该保持不变。至于我对回形针的研究,我没有找到如何做到这一点。

显然 ImageMagick 非常支持它:http://www.imagemagick.org/Usage/crop/#crop_strip 但我不知道如何将它塞进轨道上的回形针中。

非常感谢!

【问题讨论】:

    标签: ruby-on-rails image-processing imagemagick paperclip


    【解决方案1】:

    您可以用户转换图像处理的选项,以下将集中裁剪图像。

    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
    

    【讨论】:

      【解决方案2】:

      您能否将高度设置为大到离谱的高度,这样就不会出现问题了?

      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
                                             }
      

      我认为这会裁剪宽度超过 940 像素的任何内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        • 2017-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多