coolshou / thumbnailator

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

The thumbnail file size is as big as the original image #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The size of original image is 11MB, and its resolution is 3312*4416,i use the 
following code to compress original image:
Thumbnails.of(originalImage).size(600, 800).toFile(originalImage);
The size of  compressed image is also 11MB, and its resolution is 600*800. But 
if i change parameters:
Thumbnails.of(originalImage).size(90, 120).toFile(originalImage);
The size of compressed image is 2KB.
How to solve this problem? Sorry for my poor English.

Original issue reported on code.google.com by moshangc...@gmail.com on 28 Jun 2011 at 6:23

GoogleCodeExporter commented 9 years ago
Hi there,

I'm going to need more information to find out what is going on:

1. What is the value of `originalImage`? What is actual file name?
2. What is `File originalImage` pointing to? A JPEG image? A PNG image? 
Something else?
3. Are they both created from the same image file?
4. Are you running the two lines of code one after another?

One thing I did notice about the code is that the source image file and the 
destination image file is the same `File` (or `String`) so I believe that the 
thumbnail is overwriting the original image.
(That might be what you're intending, but I just wanted to double-check.)

Original comment by coobird...@gmail.com on 28 Jun 2011 at 2:47

GoogleCodeExporter commented 9 years ago
Thank you! I found the problem. Exactly as you said, it's because the source 
image file and the destination image file is the same File.

The following code works perfect:
BufferedImage image = ImageIO.read(new File("test.jpg"));
Thumbnails.of(image).size(600, 800).toFile(new File("bigPreview.jpg"));
After compressed, the size of "bigPreview.jpg" is only 80KB.

If the destination file and the source file is the same file:
BufferedImage image = ImageIO.read(new File("test.jpg"));
Thumbnails.of(image).size(600, 800).toFile(new File("test.jpg"));
Although its resolution changed, its size doesn't change.

Original comment by moshangc...@gmail.com on 29 Jun 2011 at 3:29

GoogleCodeExporter commented 9 years ago
Thank you for the follow up.

I was able to reproduce your issue for JPEG and PNG images:

  Thumbnails.of("path/to/png")
    .size(100,100)
    .toFile("path/to/png");

  Thumbnails.of("path/to/jpg")
    .size(100,100)
    .toFile("path/to/jpg");

(Reproduced on JRE 6, Windows XP, Thumbnailator 0.3.5)

It does seem odd that the file size remains the even when the image has changed.
I'll take a closer look at this issue in the next few days.

Original comment by coobird...@gmail.com on 29 Jun 2011 at 3:56

GoogleCodeExporter commented 9 years ago
This issue definitely seems to be a defect.

The following two examples should be (more or less) equivalent:

Example 1
-----------

  Thumbnails.of("path/to/png")
    .size(100,100)
    .toFile("path/to/png");

Example 2
-----------

  BufferedImage img = Thumbnails.of("path/to/png")
    .size(100,100)
    .asBufferedImage();

  ImageIO.write(img, "png", new File("path/to/png"));

However, in (1) the file size of `path/to/png` before and after the resize 
operation does not change.

Original comment by coobird...@gmail.com on 9 Jul 2011 at 7:47

GoogleCodeExporter commented 9 years ago
This issue has been addressed, and will be part of the Thumbnailator 0.3.6 
release.

------

This issue was occurring due to the way the `ImageOutputStream` returned by 
Image I/O was writing data to the beginning of the destination file, if the 
destination file already exists.

The code in question was: 

  ImageOutputStream ios = ImageIO.createImageOutputStream(destinationFile);

A workaround was to hand over a `FileOutputStream` to the 
`ImageIO.createImageOutputStream` method when obtaining an `ImageOutputStream`.

I'll close this issue once Thumbnailator 0.3.6 is released.

Original comment by coobird...@gmail.com on 9 Jul 2011 at 11:29

GoogleCodeExporter commented 9 years ago
A fix for this issue was released as part of Thumbnailator 0.3.6.

Original comment by coobird...@gmail.com on 9 Jul 2011 at 3:01