App Engine and Cloud Storage I generate thumbnail

2019-07-07 19:46发布

My function work for me but the thumbnail image file size is larger than original file size.

Original File: 30Kbonly 30KB

Thumbnail File: 50Kb

50KB, 20KB larger than original file

My Function

public static void thumbnailImage(String filename, int width, int height) throws IOException{
    GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
              .initialRetryDelayMillis(10)
              .retryMaxAttempts(10)
              .totalRetryPeriodMillis(15000)
              .build());
    AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
    ImagesService imagesService = ImagesServiceFactory.getImagesService();
    // Make an image from a Cloud Storage object, and transform it.
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + appIdentity.getDefaultGcsBucketName() +"/"+ filename);

    Image thumb = null;
    try{
        Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);          
        Transform resize = ImagesServiceFactory.makeResize(width, height);
        thumb = imagesService.applyTransform(resize, blobImage);

    }catch(Exception e){
        e.printStackTrace();
    }
    String extension = Files.getFileExtension(filename);
    filename = stripExtension(filename);
    filename = String.format("%1$s_%2$s.%3$s", filename, thumbnail, extension);

    if(thumb!=null)
    // Write the transformed image back to a Cloud Storage object.
    gcsService.createOrReplace(
        new GcsFilename(appIdentity.getDefaultGcsBucketName(), filename),
        new GcsFileOptions.Builder().mimeType("image/jpeg").acl("public-read").build(),
        ByteBuffer.wrap(thumb.getImageData()));
}

The original file is only 30KB, but the thumbnail is 50KB. They have the same V-Resolution and H-Resolution 96dpi and Bit Depth 24. Are there something else I have missed? There are a lot of similar Questions, I'm not sure if my question is duplicated.

EDIT:

this is link on google cloud

https://storage.googleapis.com/exalted-etching-131403.appspot.com/pro/abc/door-baby-bouncer.jpg

thumbnail:

https://storage.googleapis.com/exalted-etching-131403.appspot.com/pro/abc/door-baby-bouncer_thumbnail.jpg

1条回答
Animai°情兽
2楼-- · 2019-07-07 20:13

Thank tx802 and jterrace

I found the solution now. I add OutputSettings to transform function to change Encoding to JPEG

thumb = imagesService.applyTransform(resize, blobImage, new OutputSettings(OutputEncoding.JPEG));

Right now my thumbnail image is only 5Kb.

查看更多
登录 后发表回答