Imagemagick Carrierwave:将图像调整为固定宽度和高度
我有一个rails 4应用程序,允许用户上传图像。我已经验证了640X385的最小尺寸,因此最小纵横比为1.6623376623376624。现在这意味着用户不能上传更小的图片,无论是在尺寸还是纵横比方面。当他们上传更大的图像时,它会通过ImageMagick的resize_to_fit方法调整大小。但它的大小调整为一个比例的宽度和高度,我的尺寸验证失败。如果我使用resize_to_fill,即使较小的图像也会被拉伸,从而绕过纵横比的验证。那么,我如何获得调整大小的方法来将图像完美地调整到640X385尺寸?因此,我不知道您对各种尺寸的预期结果。我倾向于在命令行中使用ImageMagick,因此我制作了一个小脚本,其中包含一些可能对您有帮助,也可能没有帮助的示例:Imagemagick Carrierwave:将图像调整为固定宽度和高度,imagemagick,Imagemagick,我有一个rails 4应用程序,允许用户上传图像。我已经验证了640X385的最小尺寸,因此最小纵横比为1.6623376623376624。现在这意味着用户不能上传更小的图片,无论是在尺寸还是纵横比方面。当他们上传更大的图像时,它会通过ImageMagick的resize_to_fit方法调整大小。但它的大小调整为一个比例的宽度和高度,我的尺寸验证失败。如果我使用resize_to_fill,即使较小的图像也会被拉伸,从而绕过纵横比的验证。那么,我如何获得调整大小的方法来将图像完美地调整到64
#!/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.
有关如何使用“调整大小”的更多信息和示例:
另外,请看:
我希望这会有所帮助。告诉我,如果将1000x1000图像调整为640x385,您希望得到什么样的结果?我基本上希望在操作之前运行“调整大小以适应”功能!在MyImageUploader