Skip to content

Instantly share code, notes, and snippets.

@jlcrow
Created October 28, 2015 00:47
Show Gist options
  • Save jlcrow/6ca8179bbef6c37503f0 to your computer and use it in GitHub Desktop.
Save jlcrow/6ca8179bbef6c37503f0 to your computer and use it in GitHub Desktop.
Image Resizing Servlet
package com.photo.gallery;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageResizer extends HttpServlet {
/**
* Servlet resizes an image located in a directory in a web project ex.
* /image?root=/albums&file=/thumbs/imagename.jpg&width=270&height=100 ex.
* /image?file=/thumbs/imagename.jpg&width=270 (default root, calculated
* height)
*/
private static final long serialVersionUID = -8285774993751841288L;
public void doGet(HttpServletRequest request, HttpServletResponse response) {
// Optional: Only supports output of jpg and png, defaults to png if not
// specified
String imageOutput = getParam(request, "output", "png");
// Optional: Folder in web app where images are located, defaults to
// albums if not specified
String imageRoot = getParam(request, "root", "/albums");
// Required: Path from root to image, including filename
String imageFile = getParam(request, "file", "/Album1/image1.jpg");
// Required: Width image should be resized to
int width = Integer.parseInt(getParam(request, "width", "250"));
// Optional: If specified used, otherwise proportions are calculated
int height = Integer.parseInt(getParam(request, "width", "0"));
// Set the mime type of the image
if ("png".equals(imageOutput))
response.setContentType("image/png");
else
response.setContentType("image/jpeg");
// Server Location of the image
String imageLoc = request.getSession().getServletContext().getRealPath(imageRoot) + imageFile;
try {
// Read the original image from the Server Location
BufferedImage bufferedImage = ImageIO.read(new File(imageLoc));
// Calculate the new Height if not specified
int calcHeight = height > 0 ? height : (width * bufferedImage.getHeight() / bufferedImage.getWidth());
// Write the image
ImageIO.write(createResizedCopy(bufferedImage, width, calcHeight), imageOutput, response.getOutputStream());
} catch (Exception e) {
log("Problem with image: " + imageLoc + e);
}
}
BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight) {
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = scaledBI.createGraphics();
g.setComposite(AlphaComposite.Src);
g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
return scaledBI;
}
// Check the param if it's not present return the default
private String getParam(HttpServletRequest request, String param, String def) {
String parameter = request.getParameter(param);
if (parameter == null || "".equals(parameter)) {
return def;
} else {
return parameter;
}
}
}
<servlet>
<servlet-name>image</servlet-name>
<servlet-class>com.photo.gallery.ImageResizer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>image</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>
<img src="/image?root=/albums&file=/thumbs/imagename.jpg&width=270&height=100">
<img src="/image?file=/thumbs/imagename.jpg&width=270 ">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment