Optimize images - Losslessly compress images in Java

空扰寡人 提交于 2019-12-07 12:11:41

问题


Having an ecommerce website, We have thousands of product images. On checking pagespeed on google it shows me something like this:

I was wondering, if there is any built in feature in Java or any third party library is available with which we can losslessly compress all the images that we host. Hence we can save few KBs of our customers.

On searching through internet I found few like punnypng and kraken which are paid, hence we do not have heavy image uploaded every month, subscribing to them is not worth. I would prefer any built in feature in Java or any open source third party library.

I came across JAI, but not sure about whether it addresses this problem or not. Anyone with hands-on experience with this?

P.S. We are using Java 8


回答1:


Have you looked at classes in the javax.imageio package (http://docs.oracle.com/javase/7/docs/api/javax/imageio/package-summary.html) ?

You can do decoding and re-encoding of the images. The class ImageWriteParam (http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageWriteParam.html) lets you customize the compression settings.




回答2:


~600 KB jpeg images are quite large for screen, though not for print. Having several images on a page would mean making more or less "thumb" views being smaller. And provide an individual product page with higher resolution, say 600 KB JPEG. Standard ImageIO suffices for conversion, see @NicolaFerraro.

Faster page loading can be achieved on the overview page with multiple images, by storing the smaller views into one large image. PNG might then be appriopriate to prevent JPEG artifacts.

To provide a higher resolution for a print, one can use the CSS media setting.




回答3:


Check thumbnailator. It is great at making smaller images from larger images.

Besides you should consider when you are going to make these smaller images. At each call, at the first call (any keeping a cache), ...



来源:https://stackoverflow.com/questions/27677164/optimize-images-losslessly-compress-images-in-java

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