mystijk / thumbnailator

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

Resizing strategy #22

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I cannot use this lib which looks very good because of a missing functionnality 
: Resizing strategy.

I mean, that there is different way to generate a thumbnail, if I ask a 
thumbnail of 100x100
I'd like to choose if 
1. I will keep the ratio by fitting to the biggest dimension (but i will not 
have 100x100 result)
2. I will crop the image 
3. I will add empty area around to have exactly 100x100
4. I will not keep the ratio

As I have seen I can do 1. and 4. but I didn't see how to do 2. and/or 3.
If it exists, forgive me and please consider this as a documentation issue.

I'm sure I will use your lib later but without this functionnality I cannot in 
my current project and I'm going to code again some ugly code for resizing 
images

Original issue reported on code.google.com by brunopie...@gmail.com on 29 Aug 2011 at 3:24

GoogleCodeExporter commented 9 years ago
Thank you for the feedback.

There definitely should be more documentation for Thumbnailator, especially 
when it comes to how to deal with certain use cases.

> 2. I will crop the image 

If you want to specify a section of an image before creating a thumbnail, one 
can use the `sourceRegion` method:

  Thumbnails.of("path/to/image")
    .sourceRegion(200, 200, Positions.CENTER)
    .size(100, 100)
    .toFile("path/to/thumbnail");

The above code will take the center 200x200 region, and resize it to a 
thumbnail that is 100x100.

If what is desired is to merely crop an image, Thumbnailator does not provide a 
direct way to do so, however there is a `Canvas` image filter which can perform 
image cropping. (Please refer the javadocs at 
http://thumbnailator.googlecode.com/hg/javadoc/net/coobird/thumbnailator/filters
/Canvas.html)

> 3. I will add empty area around to have exactly 100x100

The aforementioned `Canvas` image filter has a feature to forcefully make 
thumbnails a certain size.

For example, say we take a 200x400 image and scale it down to 50x100, then we 
want to have an empty area to make a 100x100 thumbnail. The code can be written 
as follows:

  Thumbnails.of("path/to/image-200x400")
    .size(100, 100)
    .addFilter(new Canvas(100, 100, Positions.CENTER))
    .toFile("path/to/thumbnail");

Note: `size(100, 100)` will preserve the aspect ratio of the original, so the 
thumbnail without the `Canvas` filter would be 50x100.

Please let me know if there's anything I missed or anything else I can help you 
with.

Original comment by coobird...@gmail.com on 29 Aug 2011 at 3:53

GoogleCodeExporter commented 9 years ago
Thaks for your help.
I understand the point 3. and it seams good

but about the point 2. I want to take the maximum area as sourceRegion (because 
my thumbnail should look like the original by the better way) and I dont know 
the dimension of the original image.
Imagine I want a thumbnail of 100x100
if the original is 600*400 I want a sourceRegion of 400x400
if it is 200x800 I want 200x200

And without loading the image before to get its dimensions I don't see how to 
do such a thing.

maybe a method like
.croppedSourceRegion(Positions.CENTER)
the true size of the sourceRegion will have to be calculated by using the ratio 
height/width given by .size and the true size of the image when loaded.
I'm sad, not having time to look more into your code and propose a true solution

Original comment by brunopie...@gmail.com on 30 Aug 2011 at 9:48

GoogleCodeExporter commented 9 years ago
If I understand you correctly, perhaps having a way to specify the *minimum* 
dimensions* that the thumbnail should be could help in your situation?

For example, let's say you want a thumbnail that is 100x100, and the original 
image is 400x200.

If Thumbnailator could resize the image to have the *minimum dimensions* of 
100x100, it should first resize the image to 200x100. Then, if the 100x100 
thumbnail should be created by cropping the image, the `Canvas` filter could be 
used to achieve that.

Hypothetically, the call could be like the following:

  Thumbnails.of("path/to/image")
    .minimumSize(100, 100)
    .addFilter(new Canvas(100, 100, Positions.CENTER))
    .toFile("path/to/thumbnail");

Original comment by coobird...@gmail.com on 1 Sep 2011 at 3:47

GoogleCodeExporter commented 9 years ago
Yes it would do the job.

If it was me, I would set direct methods to the 4 way simple way of 
thumbnailing. But maybe good documentation about this may be enougth.

Thank again for your replies

Original comment by brunopie...@gmail.com on 1 Sep 2011 at 4:15

GoogleCodeExporter commented 9 years ago
*Note to self*

I'm going to add this as a note that Issue 24 seems to be a similar request to 
the resizing strategy #2.

Maybe adding a `crop` method to handle a call to the `Canvas` filter would be a 
better solution.

Original comment by coobird...@gmail.com on 29 Oct 2011 at 3:36