How to compress images with Google App Engine Blobstore

主宰稳场 提交于 2019-12-21 20:47:31

问题


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.


回答1:


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);



回答2:


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




回答3:


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




回答4:


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).



来源:https://stackoverflow.com/questions/13728233/how-to-compress-images-with-google-app-engine-blobstore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!