Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress images with Google App Engine Blobstore

We have an app that servers a series of images from blobstore. An example is here:

http://lh4.ggpht.com/f76xUkRZLRkb_Qz5uu82TX3LoBRh4eYb9hxYwMRMLCk5ghO_OL0DW2v4rRnkewUyDWfBuBttgbUvuJJXwtFQosEB=s0

It was a huge png, so this downloads at 536K.

If we resize it to 400 across, it's still huge (263k):

http://lh4.ggpht.com/f76xUkRZLRkb_Qz5uu82TX3LoBRh4eYb9hxYwMRMLCk5ghO_OL0DW2v4rRnkewUyDWfBuBttgbUvuJJXwtFQosEB=s400

How can we request or store the picture in some kind of better compression? We have a mobile client for our app, and waiting through 273K is making it really slow.

like image 857
Chris Cartland Avatar asked Dec 20 '25 05:12

Chris Cartland


1 Answers

There is Images API. To compress and resize image:

// get image from blobstore
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image oldImage = ImagesServiceFactory.makeImageFromBlob(blobKey); 

// compress & resize it
OutputSettings settings = new OutputSettings(ImagesService.OutputEncoding.JPEG);
settings.setQuality(90);
Transform transform = ImagesServiceFactory.makeResize(newWidth, newHeight) 
Image newImage = imagesService.applyTransform(transform, oldImage, settings);
byte[] blobData = newImage.getImageData();

//save data to blobstore
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile("image/jpeg", someFilename);
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
writeChannel.write(ByteBuffer.wrap(blobData));
writeChannel.closeFinally();

// get the blobKey to newly written data
BlobKey blobKey = fileService.getBlobKey(file);
like image 71
Peter Knego Avatar answered Dec 22 '25 20:12

Peter Knego



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!