简体   繁体   English

如何制作回形针而不缩放附件图像?

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

I want Paperclip to crop, and not scale (see :full1 in this excerpt) 我希望回形针可以裁剪而不是缩放(请参见本摘录中的: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
                                       }

I want :full1 to work, but it doesn't. 我希望:full1可以工作,但事实并非如此。 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. 原因是我正在上传网络屏幕截图,并且希望将它们的宽度(从中心)调整为940像素,但高度应保持不变。 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. 显然,它受到ImageMagick的完全支持: http : //www.imagemagick.org/Usage/crop/#crop_strip但我不知道如何将其塞入导轨中的回形针中。

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. 我认为这会裁切任何大于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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