My function work for me but the thumbnail image file size is larger than original file size.
Thumbnail File: 50Kb
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:
Thank tx802 and jterrace
I found the solution now. I add OutputSettings to transform function to change Encoding to JPEG
Right now my thumbnail image is only 5Kb.