0

Refer to http://www.playframework.org/community/snippets/4

Is it possible to compress/resize the Blob image before rendering it on screen?

public static void getPicture(long id) {
   Picture picture = Picture.findById(id);
   response.setContentTypeIfNotSet(picture.image.type());
   renderBinary(picture.image.get());
}
lemon
  • 9,155
  • 7
  • 39
  • 47

2 Answers2

2

You can use Image class in the play.libs package, which has a static method called resize.

The signature is

resize(java.io.File originalImage, java.io.File to, int w, int h) 

You obviously need to go through extra steps to save the image to a File, to then perform the conversion, but if this is too much, you may be able to take a copy of the code and make it work with a Blob instead.

Alternatively, if there is any other third party library that works with the Blob type, then this could easily be integrated.

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
  • Note that when using this method, you may loose rotation information (see http://stackoverflow.com/questions/9453367/is-javax-imageio-imageio-broken-it-imports-some-images-as-rotated for more on that). – Samuel Aug 09 '12 at 11:29
0

If you are handling JPEG or other compressed images, you won't save much disk/bandwidth compressing the image. For the most cases you will probably just get some bytes less which are not worth the compression cost.

For resize JMagick (which is a interface for ImageMagick) is a good option. There is a small tutorial about how to resize images using JMagick.

Hope that helps

marcospereira
  • 12,045
  • 3
  • 46
  • 52