Image resize servlet

Resize image servlet using imagemagick or thumbnailator.

View the Project on GitHub soulgalore/image-resize-servlet

Image resize servlet Build Status

Resize an image using imagemagick (http://www.imagemagick.org/) or thumbnailator (http://code.google.com/p/thumbnailator/) from within a servlet. The servlet will resize an already existing original image to one of the predefined desired sizes (or whatever size you want, depending on configuration).

What you need

  1. Imagemagick needs to be installed (http://www.imagemagick.org/script/binary-releases.php or by homebrew if you use Mac OS X) and the user of the servlet engine needs to have imagemagick on the path. You need it for all tests to work, even if you will only run thumbnailator.
  2. Simple as that :)

How to make a test run in Eclipse

  1. Checkout the project
  2. Make the project a Maven project ("Convert to Maven project")
  3. Start the Tomcat using https://github.com/jsimone/webapp-runner#create-a-launch-configuration
  4. Access http://localhost:8080/thumbs/test-460x360.png and if you see the image, it works.

How to use it

  1. Setup the servlet in your web.xml (you can configure the request parameter name, original image folder, thumbnail base folder and a list of valid sizes of images)
  2. Access the servlet with the your predefined request parameter with the value of the image you want. /SERVLET/?img=MY_ORIGINAL_IMAGE-120x94.png
  3. The images is returned with that size, resized from the file MY_ORIGINAL_IMAGE.png

How it works

The servlet will check if the image already exist in the requested size. If the file exists, it is forwarded to the user. Else the servlet checks if the original image exist (named MY_ORIGINAL_IMAGE.png) and that the requested thumbnail size is valid (you can configure valid size or say that all sizes are valid) . If the request is valid, a new thumbnail is resized and put in your configured thumbnail base dir + a generated folder path that is calculated from the original file name, so that files are spread within the file system (but all sizes for a specific file, are within the same folder). Then the new image is returned to the user.

The calling thread is the thread that creates the actual thumbnail and it is concurrent in the meaning only one thread can create the same thumbnail.

Extras

Bundled with Tomcat, set it up using and inctructions of how to start: https://github.com/jsimone/webapp-runner

Also an expire filter is setup in the web.xml to set some cache headers (so make sure to empty your browser cache if you change images but keep the same name).

Tuckeys url rewrite filter is used to setup a friendly url.

If you want to run this in production, you need to think of a couple of things:

  1. Change the original and thumbnail directories so they exists outside the webapp (you will probably want the images to live even if you redeploy :) ). If you use Tomcat, configure it by allowLinking to be able to symlink outside your webapp.
  2. Make sure you set correct cache headers and add a layer in front of your servlet runner (Nginx/Apache etc) that can cache the generated image.
  3. Configure your logging backend for slf4j.