【问题标题】:Paperclip not sizing correctly回形针尺寸不正确
【发布时间】:2014-10-08 21:56:59
【问题描述】:

我在我的应用程序中使用 Paperclip,但图像不符合我设置的尺寸。我正在使用回形针和 rmagick 并安装了 ImageMagik。当我运行 where convert 我得到了

C:\Program Files\ImageMagick-6.8.9-Q16\convert.exe
C:\Windows\System32\convert.exe

开发.rb

Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.9-Q16/convert.exe"

宝石文件

gem 'paperclip'
gem 'rmagick', '~> 2.13.2', :platforms => :ruby

用户.rb

has_attached_file :avatar, styles: { medium: '210x260>', larger: "300x300>", thumb: "100x100>" }, default_url: "/assets/default.png"
validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

查看

<%= image_tag @user.avatar(**style**) %>

我正在两个不同尺寸的不同图像上测试它,A(最初:960x688)和 B(最初:160x160)

A(:thumb) 变成 100x72

B(:thumb) 变成 100x100

A(:medium) 变为 210x151

B(:medium) 变为 160x160

A(:larger) 变为 300x300

B(:larger) 变为 300x300

我尝试在更改大小后重新上传图像,但得到相同的结果。那么,回形针是否对不同尺寸的图像有问题,还是我的代码有问题?

【问题讨论】:

  • 尝试在末尾加上 # 而不是 >
  • 修复了它们,但每次更改样式时我都必须重新上传图像。如果我必须在其他地方更改样式,我是否必须继续上传图片?

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip


【解决方案1】:

您需要在大小尺寸之后更改尾随字符,因此您需要

medium: 210x260#

文档明确说明

Default behavior is to resize the image and maintain aspect ratio (i.e. the :medium version of a 300×150 image will be 200×100). Some commonly used options are:

trailing '#', thumbnail will be centrally cropped, ensuring the requested dimensions.
trailing '>', thumbnail will only be modified if it is currently larger requested dimensions. (i.e. the :small thumb for a 120×80 original image will be unchanged)

对于您当前的图像,您可以在控制台中这样做

Image.all.each {|s| s.image.reprocess! }

在此之后,您上传的任何其他图像都不需要重新处理,并且会根据您的需要调整大小

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多