How to compress images with Google App Engine Blob

2020-07-21 05:16发布

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.

4条回答
Lonely孤独者°
2楼-- · 2020-07-21 05:33

You can use PIL to serve the re-compressed image dynamically from your frontend instance.

查看更多
小情绪 Triste *
3楼-- · 2020-07-21 05:44

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);
查看更多
干净又极端
4楼-- · 2020-07-21 05:44

You can enable Pagespeed service to re-compress images on the fly.

查看更多
Viruses.
5楼-- · 2020-07-21 05:54

You can upload differents version (with different compression ratio) of your images to the blobstore or cloud storage and select the version to display client side (using CSS or client side logic).

查看更多
登录 后发表回答