I have a rails 4 app, where I allow users to upload images. I have validations in place for min dimension of 640X385 and thus a min aspect ratio of 1.6623376623376624. Now that means user can't upload smaller images either in terms of dimension or aspect ratio. And when they upload a bigger image, it gets resized by ImageMagick's resize_to_fit method. But it resizes to a proportional width and height and my dimension's validation fails. If I use resize_to_fill, even the smaller images get stretched and bypass aspect ratio's validation. So How do I get the resize method to resize an image perfectly to 640X385 dimension?
0
votes
Tell me, what results do you expect if you resize a 1000x1000 image to 640x385?
– Mark Setchell
I basically want the resize_to_fit to run BEFORE manipulate! in class MyImageUploader < BaseUploader
– Vivek Tripathi
You didn't answer me. I asked you what it would look like, you told me when it would get processed.
– Mark Setchell
Actually i have another image that i will be overlapping over the uploaded image. Now the problem is the overlapping image is fixed in size and dimensions. So I want my images to be too of a certain dimension. And thus I want uploaded images to be of 640X385 resolution.
– Vivek Tripathi
1 Answers
0
votes
So I do not know exactly what result you expect for various sizes. I tend to use ImageMagick on the commandline so I produced a small script with examples that may or may not help you:
#!/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
The output is as follows:
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
My guess is that you are looking for the last variant where the output is named r3.<something>
.
For more info and good examples of how to use resize: http://www.imagemagick.org/Usage/resize/
Also have a look at: Resizing the image with padding using convert on ubuntu
I hope this helps.