iFrankWu / thumbnailator

Automatically exported from code.google.com/p/thumbnailator
Other
0 stars 0 forks source link

forceSize Not maintaining the Aspect Ratio of thr original image #72

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Hi I have an image and I wanted to resize it to a fixed width and height. But 
Still it needs to maintain the same aspect ratio of the original image

Thumbnails.of(new File(originalImage))
.forceSize(150, 100)
.toFile(new File("p.jpg"));

What is the expected output? What do you see instead?
Maintaining the same aspect ration if the image is resized.

What version of the product are you using? On what operating system? Which
version of Java (Sun/Oracle? OpenJDK?) ?

JDK 1.7

Please provide any additional information below.

Original issue reported on code.google.com by saravan....@gmail.com on 7 Oct 2014 at 2:12

GoogleCodeExporter commented 9 years ago
Thank you for submitting an issue.

The behavior you describe is intended. The `forceSize` method will forcefully 
resize to the exact dimensions you specify, therefore, in most situations, the 
aspect ratio of the image will not be preserved. This is documented in the 
javadocs[1]:

> The thumbnails will be forced to the specified size, therefore, the aspect 
ratio of
> the original image will not be preserved in the thumbnails.

Since forcing the size to the specific dimensions is probably not what you 
want, I'm going to guess you either want to (a) crop the image or (b) fill 
empty area with some color.

In case (a), you'll be able to use the `crop` method [2], which preserve the 
aspect ratio, but crops out any portion that will lie outside of the given 
dimensions. Explanation of the `crop` method in action can be found here: 
https://code.google.com/p/thumbnailator/wiki/Changes#Thumbnailator_0.4.0_%28Febr
uary_11,_2012%29

In case (b), there's a `Canvas` filter [3] which will perform the resize while 
maintaining the aspect ratio, but portion of the image which is "empty" will be 
filled with a color of your choice. An example code of using the `Canvas` 
filter is the following:

  Thumbnails.of(originalImage)
    .size(150, 100)
    .addFilter(150, 100, Color.blue)
    .toFile(thumbnailImage);

In the above code, the original image will be sized to fit the 150 x 100 area 
with the `size` method, but if the image becomes smaller than 150 x 100, then 
the area that is empty is solid filled with blue.

For example, 300 x 100 image will be resized to 150 x 50, however the height is 
50, so to grow the final image to height of 100, that difference (empty area) 
is filled with a blue color.

I hope one of the above cases matches what you're expecting to get out of 
Thumbnailator.

Let me know if you're expecting something else. Thanks!

[1] 
http://thumbnailator.googlecode.com/hg/javadoc/net/coobird/thumbnailator/Thumbna
ils.Builder.html#forceSize(int,%20int)
[2] 
http://thumbnailator.googlecode.com/hg/javadoc/net/coobird/thumbnailator/Thumbna
ils.Builder.html#crop(net.coobird.thumbnailator.geometry.Position)
[3] 
http://thumbnailator.googlecode.com/hg/javadoc/net/coobird/thumbnailator/filters
/Canvas.html

Original comment by coobird...@gmail.com on 7 Oct 2014 at 2:42

GoogleCodeExporter commented 9 years ago
Hi,

Thanks for the quick response. I will go with the option B. However when I
tried your code. It is showing compilation issues. Can you please share
with a code snippet as how to use filters.

Original comment by saravan....@gmail.com on 7 Oct 2014 at 4:01

GoogleCodeExporter commented 9 years ago
Yikes, sorry for the mistake.

This should work:

  Thumbnails.of(originalImage)
    .size(150, 100)
    .addFilter(new Canvas(150, 100, Positions.CENTER, Color.blue))
    .toFile(thumbnailImage);

By changing which `Positions` enum you use, the position where the image is 
positioned can be changed.

http://thumbnailator.googlecode.com/hg/javadoc/net/coobird/thumbnailator/filters
/Canvas.html

Original comment by coobird...@gmail.com on 7 Oct 2014 at 4:06

GoogleCodeExporter commented 9 years ago
Thank you it worked charmingly.

Original comment by saravan....@gmail.com on 7 Oct 2014 at 5:49

GoogleCodeExporter commented 9 years ago

Original comment by coobird...@gmail.com on 8 Oct 2014 at 12:52