The Paperclip plugin for Rails has a resize option that keeps the image in proportion. According to Ryan Bates' Paperclip Railscast, to make sure that option is on, you have to add a greater-than sign in the end of the size for the style you're looking to resize, as such:

:styles => { :small => "160x160>" }

I'm looking for Paperclip to resize my image to an exact size, even if that means it being out of proportion, so I figured that removing the greater-than sign would do the trick, as such:

:styles => { :small => "160x160" }

Well, turns out nothing happens. So the greater-than sign is redundant in the first place. I'm still left with the following question, though:

How would I force the image into a set size, ignoring proportions?

Thanks!

推荐答案

You can use "160x160#" which will scale and crop to exactly that size, which is unique to paperclip. Otherwise you can use any of the ImageMagick geometry strings, detailed here:

ImageMagick Geometry

But I'll quote the one you're interested in:

"160x160!"

Width and height emphatically given, original aspect ratio ignored.

Ruby-on-rails相关问答推荐

为什么在使用 Ruby on Rails 时我的徽标没有出现在 Bootstrap 导航栏中?

如何让删除链接响应 Rails 7 中的 turbo_stream 和 html?

如何保持 en.yml DRY?

ruby on rails 中的自定义关联

Rails - 获取没有 GET 参数的当前 url

在 Rails/ActiveRecord 列名中使用问号字符

如何将字符串转换为类方法?

Rspec/Capybara 正在加载,循环要求被认为是有害的

关于在不适合任何地方的 Rails 应用程序中放置类的指南

批量查找 mongoDB 记录(使用 mongoid ruby​​ 适配器)

获取磁盘上 ActiveStorage 文件的路径

如何让 ActiveAdmin 使用强参数?

如何在 Ruby 中生成随机日期?

导轨链接到:远程

在范围内传递参数

何时使用 Helpers 而不是 Partials

Ruby on Rails 3:通过 Rails 将数据流式传输到客户端

使用 ActiveRecord 获取列名

Rails:在 link_to 中保留 GET 查询字符串参数

RoR select_tag 默认值和选项