简体   繁体   中英

Resize images on server for various devices

I'm looking to solve the most common image re-sizing problem. I have a Java Web application where user uploads a high quality image approximately 2 MB and this image is accessed from multiple clients ranging from mobile app, mobile site and web application (Eg: any eCommerce application has thumbnail, small size, large size of the same image). This image shall be resized on upload and will be stored on the server.

I want to resize this uploaded image into multiple images of smaller size for the clients mentioned above. Is there any open source or commercial library which can automatically resize the images.

Just wondering how would these eCommerce applications would do this ?

There are many Java image processing frameworks for doing this.

The code below uses Marvin Framework to resize the image to different resolutions using the width as reference. Height is calculated to keep proportion:

MarvinImage image = new MarvinImage(uploadedImage);
String fileName = "myfilename.jpg";
MarvinImage scaled = new MarvinImage(1,1)

scale(image, scaled, 1000); // width == 1000
MarvinImageIO.saveImage(scaled, new File(URI.create("file:///server_storage/"+FilenameUtils.removeExtension(fileName)+"_w1000.jpg")).getAbsolutePath());
scale(image, scaled, 500); // width == 500
MarvinImageIO.saveImage(scaled, new File(URI.create("file:///server_storage/"+FilenameUtils.removeExtension(fileName)+"_w500.jpg")).getAbsolutePath());
scale(image, scaled, 250); // width = 250
MarvinImageIO.saveImage(scaled, new File(URI.create("file:///server_storage/"+FilenameUtils.removeExtension(fileName)+"_w250.jpg")).getAbsolutePath());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM