RoR - 回形针 - 如何设置附件的最小宽度

发布于 2024-08-29 09:07:45 字数 341 浏览 7 评论 0原文

我的布局要求是将所有缩略图保持在 80px 高度,不更高,不更小。在我的模型中,我将样式设置为 :thumb=> “500x80>”,所以基本上几乎所有不太宽的图片都会得到其高度为 80px 的完美缩影。然而,有时我的图片又窄又高,因此拇指可能具有无法点击的尺寸,例如 5x80。因此,只要缩略图没有变得太窄,我就不想裁剪图片,但我认为如果拇指的宽度变得小于 25px,我可以做出一点牺牲并裁剪它们。

所以我的问题是 - 是否可以在回形针中设置图片的最小比例,其中样式将是 "500x80>" ,超出范围它将变成诸如 "25x80#" ?

my layout's requirement is to keep all thumbnails at 80px height, not higher, not smaller. In my model I set the style to :thumb=> "500x80>", so basically almost every picture which is not too wide gets its perfect miniature with 80px height. Sometimes, however, my pictures are narrow and high, so the thumb can have unclickable dimensions of like 5x80. So I dont want to crop pictures as long as thumbnails are not getting crazy narrow, but I think I can make a little sacrifice and crop them if thumb's width is getting smaller than 25px.

So my questions is - is it possible in paperclip to set minimal proportions of a picture by which the style will be "500x80>" and beyond that it will turn to sth like "25x80#"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹短裙飘 2024-09-05 09:07:45

几周前我在互联网上找到了一个很好的解决方案。忘记在哪里了,抱歉。但它看起来像这样:

has_attached_file :img, :styles => {:thumb => [Proc.new { |instance| instance.resize }, :jpg]}


def resize     
@geo_original = Paperclip::Geometry.from_file(img.to_file(:original))

ratio = @geo_original.width/@geo_original.height  

if ratio < 0.4 or ratio > 1.375
    # Image very high or very wide
    "110x80#"   
else
    # Average dimensions
    "110x80>"
end
end

I found a nice solution somewhere in the internet a couple of weeks ago. I forgot where, sorry. But it looks like this:

has_attached_file :img, :styles => {:thumb => [Proc.new { |instance| instance.resize }, :jpg]}


def resize     
@geo_original = Paperclip::Geometry.from_file(img.to_file(:original))

ratio = @geo_original.width/@geo_original.height  

if ratio < 0.4 or ratio > 1.375
    # Image very high or very wide
    "110x80#"   
else
    # Average dimensions
    "110x80>"
end
end
计㈡愣 2024-09-05 09:07:45

我不确定你如何仅使用回形针来完成此任务 - 感觉应该有某种方法可以做到这一点,不是吗?

Paperclip 只是在后台使用 imagemagick (http://www.imagemagick.org/Usage/ resize/#shrink)你可以定时执行一个作业,使用图像魔法在每晚的基础上增长那些讨厌的狭窄图像。

这是一个黑客,但我能提供的最好的主意。

祝你好运。

I'm not sure how you could accomplish this using just paperclip - feels like there should be someway to do it doesn't it?

Paperclip is just using imagemagick in the background (http://www.imagemagick.org/Usage/resize/#shrink) you could cron a job that uses image magic to grow those pesky narrow images on a nightly basis.

It's a hack, but best idea I can offer.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文