【问题标题】:Carrierwave: resize image to fixed width and heightCarrierwave:将图像大小调整为固定的宽度和高度
【发布时间】:2015-05-19 19:24:54
【问题描述】:

我有一个 rails 4 应用程序,允许用户上传图片。我对 640X385 的最小尺寸进行了验证,因此最小纵横比为 1.6623376623376624。现在这意味着用户无法上传尺寸或纵横比方面的较小图像。当他们上传更大的图像时,它会通过 ImageMagick 的 resize_to_fit 方法调整大小。但它会调整到成比例的宽度和高度,并且我的尺寸验证失败。如果我使用 resize_to_fill,即使是较小的图像也会被拉伸并绕过纵横比的验证。那么如何获得 resize 方法将图像完美调整为 640X385 尺寸?

【问题讨论】:

  • 告诉我,如果您将 1000x1000 的图像调整为 640x385,您期望得到什么结果?
  • 我基本上希望 resize_to_fit 在操作之前运行!在类 MyImageUploader
  • 你没有回答我。我问你它会是什么样子,你告诉我什么时候它会被处理。
  • 实际上我有另一个图像,我将与上传的图像重叠。现在的问题是重叠图像的大小和尺寸是固定的。所以我希望我的图像具有一定的维度。因此我希望上传的图片分辨率为 640X385。

标签: imagemagick


【解决方案1】:

所以我不知道您对各种尺寸的期望结果究竟是什么。我倾向于在命令行上使用 ImageMagick,所以我制作了一个小脚本,其中包含可能对您有帮助也可能没有帮助的示例:

#!/bin/bash

# cleanup
rm *.jpg

# as expected
convert -size 640x385 xc:white img0.jpg
# bigger aspect ratio (2.0)
convert -size 800x400 xc:white img1.jpg
# lower aspect ratio (1.0)
convert -size 800x800 xc:white img2.jpg
# bigger aspect ratio (2.0) but to small
convert -size 400x200 xc:white img3.jpg
# lower aspect ratio (1.0) but to small
convert -size 200x200 xc:white img4.jpg

for img in img*; do
    # resize images to fit in box but keep aspect ratio
    convert ${img} -resize 640x385 r1.${img}
    # resize images keeping the aspectratio to fit in box. But only shrink
    # images, images that are to small is not enlarged.
    convert ${img} -resize 640x385\> r2.${img}
    # resize images ignoring the aspectratio to fit in box. But only shrink
    # images, images that are to small is not enlarged.
    convert ${img} -resize 640x385\>\! r3.${img}
done

identify  -format "%f: %w %h\n" *.jpg

输出如下:

img0.jpg: 640 385
img1.jpg: 800 400
img2.jpg: 800 800
img3.jpg: 400 200
img4.jpg: 200 200
r1.img0.jpg: 640 385
r1.img1.jpg: 640 320
r1.img2.jpg: 385 385
r1.img3.jpg: 640 320
r1.img4.jpg: 385 385
r2.img0.jpg: 640 385
r2.img1.jpg: 640 320
r2.img2.jpg: 385 385
r2.img3.jpg: 400 200
r2.img4.jpg: 200 200
r3.img0.jpg: 640 385
r3.img1.jpg: 640 385
r3.img2.jpg: 640 385
r3.img3.jpg: 400 200
r3.img4.jpg: 200 200

我的猜测是您正在寻找输出命名为 r3.<something> 的最后一个变体。

有关如何使用调整大小的更多信息和良好示例:http://www.imagemagick.org/Usage/resize/

还可以看看: Resizing the image with padding using convert on ubuntu

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-01-24
    • 2012-08-14
    • 2014-08-17
    • 2014-01-16
    • 2013-04-28
    • 2013-11-03
    • 2011-07-10
    • 1970-01-01
    相关资源
    最近更新 更多