coolshou / thumbnailator

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

Today, thumbnailator doesn't permit to create a thumbnail keeping aspect ratio AND forcing a defined size #24

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I need to create thumbnails similar to the Facebook ones. What Facebook do is 
maintain the aspect ratio, but forcing a defined size of thumbnail.

It resizes the image to the defined size and then cuts what is left, forcing 
the height or the width (and avoiding blank spaces around the image). For me is 
the best approach when we talk about thumbnails.

I tried to use something like this:

Thumbnails.of(myInputStream).forceSize(width, 
height).keepAspectRatio(true).outputFormat("jpg").outputQuality(1.0f).toOutputSt
ream(myOutputStream);

But it does not work. I think this approach is not possible yet with 
thumbnailator so can you implement it? Just a suggestion to make this library 
even better (it is definately the best and simplest all over the web).

Thanks in advance.

Original issue reported on code.google.com by lua...@gmail.com on 13 Oct 2011 at 8:06

GoogleCodeExporter commented 9 years ago
It is not a defect. It would be kind of an improvement I think, but I do not 
know how to change the type of the issue.

Cheers.

Original comment by lua...@gmail.com on 13 Oct 2011 at 8:14

GoogleCodeExporter commented 9 years ago
Sorry for the late reply.

Let me see if I understand your request correctly.

You'd like to see a feature where one would be able to:
  1. define the minimum dimensions that the thumbnail should resize to, then
  2. crop out a portion of the resized image to the dimension you desire

With a concrete example -- starting from a 400x600 image, we want to end up 
with a final image of 200x200. 
  1. The first step should resize the image so the image will be at least 200x200, so the resized image will be 200x300.
  2. The second step will be to crop out a 200x200 region of the 200x300 image.

Please let me know if this is what is being requested.

----------

Notes by the developer about the potential API design.

Candidate 1:

  Thumbnails.of("path/to/image")
    .minimumSize(200, 200)
    .crop(Positions.CENTER)
    .toFile("path/to/thumbnail");

 * Use the `minimumSize` method currently being proposed for Issue 22.
 * The downside is that it is unintuitive.

or

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

 * This would be more intuitive.
 * However, the behavior of the `size` method will be overriden so that the image will no longer be resized to be a *maximum* of 200x200, but it will be sized so that the minimum dimension will be 200x200.

Another potential design consideration is whether to have dimension arguments 
on the `crop` method, i.e. `crop(int width, int height, Position position)`

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

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago
Yes. Your examples are perfect.

This is exactly what I meant.

About the "crop" function: I think it would be a great improvement too.

Thanks for you reply.

And, again, congratulations for your work. Thumbnailator is a great library.

Original comment by lua...@gmail.com on 29 Oct 2011 at 3:42

GoogleCodeExporter commented 9 years ago
Thank you for your kind words. :)

Comments like your's have made working on Thumbnailator very worthwhile and 
rewarding. Thank you!

Original comment by coobird...@gmail.com on 13 Nov 2011 at 6:17

GoogleCodeExporter commented 9 years ago
Exactly the feature I'm looking for as well. Look forward to seeing the 
updates. Thanks!

Original comment by simonc...@gmail.com on 15 Nov 2011 at 12:56

GoogleCodeExporter commented 9 years ago
Hi,

I had this requirement too .. This can be done now as follows:

//required dimensions of thumb
dw = ?
dh = ?
double da = dw / dh;
//get orig
BufferedImage orig = ImageIO.read(new File("origimage"));

int sw = orig.getWidth();
int sh = orig.getHeight();
double sa = sw / sh;

BufferedImage thumb = null;
// crop? and scale
if (sa > da) {
  // too wide
  double tw = sh * da;
  int x = (int) (sw - tw) / 2;
  int y = 0;
  thumb = Thumbnails.of(orig).sourceRegion(x, y, (int) tw, sh)
   .size(dw, dh).asBufferedImage();
} else if (sa < da) {
  //too tall
  double th = sw / da;
  int x = 0;
  int y = (int) (sh - th) / 2;
  thumb = Thumbnails.of(orig).sourceRegion(x, y, sw, (int) th).size(dw, dh).asBufferedImage();
} else {
  //same aspect ratio ... scale
  thumb = Thumbnails.of(orig).size(dw, dh).asBufferedImage();
}

if(thumb != nul){
...
}

Original comment by rosh...@gmail.com on 20 Jan 2012 at 11:58

GoogleCodeExporter commented 9 years ago
Hello,

Thanks for your reply!

I'm on vacation now, but I'll try it soon as possible.

Regards,

    Luan Mattner Müller

Original comment by lua...@gmail.com on 3 Feb 2012 at 4:44

GoogleCodeExporter commented 9 years ago

Original comment by coobird...@gmail.com on 5 Feb 2012 at 8:12

GoogleCodeExporter commented 9 years ago

Original comment by coobird...@gmail.com on 5 Feb 2012 at 5:20

GoogleCodeExporter commented 9 years ago
This issue has been addressed by the addition of the `crop` method in 
Thumbnailator 0.4.0:

http://thumbnailator.googlecode.com/hg/javadoc/net/coobird/thumbnailator/Thumbna
ils.Builder.html#crop(net.coobird.thumbnailator.geometry.Position)

For example, resizing a image to 100x100 while cropping out the center portion 
of the image, one can use the following code:

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

For a visual representation of what takes place, please take a look at the 
image presented in the "Changes" page for the Thumbnailator 0.4.0 release:

http://code.google.com/p/thumbnailator/wiki/Changes#Thumbnailator_0.4.0_(Februar
y_11,_2012)

Original comment by coobird...@gmail.com on 11 Feb 2012 at 7:15

GoogleCodeExporter commented 9 years ago
That's perfect!

I will try this new functionality soon as possible!

Thanks for the attention on this issue!

Best regards,

    Luan Mattner Müller

Original comment by lua...@gmail.com on 11 Feb 2012 at 7:22

GoogleCodeExporter commented 9 years ago
Thank you + 1

Original comment by simonc...@gmail.com on 11 Feb 2012 at 7:27

GoogleCodeExporter commented 9 years ago
I don't want to keep aspect ration how can I acheive that.

// gives me image with aspect ratio.
//Input image dimension 1366x768
BufferedImage img = Thumbnails.of(bufferedImage).size(65, 65).asBufferedImage();
// getting image of 65x37

//saving it by 
save(new File(loc), img);

public void save(File file, BufferedImage image2) {
        this.filename = file.getName();
        String suffix = filename.substring(filename.lastIndexOf('.') + 1);
        suffix = suffix.toLowerCase();
        if (suffix.equals("jpg") || suffix.equals("png") || suffix.equals("gif")) {
            File f2 = new File(file.getPath());
            if (f2.exists() == false) {
                f2.mkdirs();
            }
            if (file.exists()) {
                file.delete();
            }
            try {
                ImageIO.write(image2, suffix, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Error: filename must end in .jpg or .png");
        }
    }

Original comment by DevOrg...@gmail.com on 27 Dec 2014 at 11:16

Attachments:

GoogleCodeExporter commented 9 years ago
Hello DevOrg,

Trying to deduce your intent from the attached image, I'm guessing that you do 
indeed want to keep the aspect ratio of the original image, but want to have it 
cropped in the center so that the final image will be 65x65.

In order to do so, you can use the `crop` method, such as:

  BufferedImage img = 
    Thumbnails.of("path/to/image")
      .size(65, 65)
      .crop(Positions.CENTER)
      .asBufferedImage();

By doing the above, you'll be able to end up with an image which is similar to 
the thumbnail images shown at the bottom of the attached picture.

I hope that's what you're trying to achieve.

Also, in the future, if you have a question or an issue, please open a new 
issue, as that would be a more appropriate place to address your issue than to 
add to a feature-request ticket which has been closed 3 years ago -- Thank you!

Original comment by coobird...@gmail.com on 10 Jan 2015 at 8:41