Search code examples
javaamazon-web-servicesamazon-s3thumbnailsimage-compression

Can I compress a 1MB image file (PNG, JPG, TIFF, etc) into a ~1KB thumbnail?


As per our project requirements we need to save artworks (images with JPG, PNG, TIFF etc image formats) which can size anywhere from 1KB to 1MB. We are now storing them in AWS S3 bucket.

I have below few questions on our implementation:

  1. Is it a good solution to store all the images in AWS S3 bucket?
  2. Can I directly fetch a thumbnail from AWS S3 bucket for all the image files? If yes, how can I fetch that thumbnail?
  3. Can I create a thumbnail (of size ~1KB) every time I upload an image and store the byte array in RDBMS? (This is just to avoid fetching the image content from S3 bucket, because those thumbnails are used just for preview purposes)
  4. Can I compress the images in java? How to do that so that I get the least minimal byte size?

Solution

  • import java.awt.*;
    import javax.imageio.*;
    import org.imgscalr.Scalr;
    
    void createThumbnailUsingScalr(File sourceImage) throws Exception {
            File destImage =
                    new File("/Path/For/Destination/Thubnail.png");
                BufferedImage img = ImageIO.read(sourceImage);
                BufferedImage thumbImg = Scalr.resize(img, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, 200, 200, Scalr.OP_ANTIALIAS);
                ImageIO.write(thumbImg, "PNG", destImage);
        }
    

    This above code is little bit helping my needs