Thumbnailator is an excellent Google open source Java library for image processing. The processing effect is far better than that of the Java API. The processing is simplified from the classes that provide the existing image files and image objects from the API, two or three lines of code can generate the processed image from the existing image, and allows fine-tuning how the image is generated, while maintaining the need to write Minimum amount of code. It also supports batch processing operations on all images in a directory.
Supported processing operations: image scaling, region cropping, watermarking, rotation, maintaining ratio.
It is also worth mentioning that Thumbnailator is still constantly updated, how about it, it feels very secure!
Thumbnailator official website: http://code.google.com/p/thumbnailator/
Here we introduce how to use Thumbnailator
to use the introduction address:
http://blog.csdn.net/chenleixing/article/details/44685817
http://www .qzblog.net/blog/220
http://blog.csdn.net/wangpeng047/article/details/17610451 Thumbnail compressed file jar package <!-- Image thumbnail --> <dependency> <groupId>net.coobird </groupId> <artifactId>thumbnailator</artifactId> <version>0.
</dependency> Scale the picture according to the specified size (it will follow the original picture height and width ratio) //Scale and resize the picture according to the specified size (will follow the original picture height and width ratio) //Here, the picture is compressed to 400× 500 thumbnail Thumbnails.of(fromPic).size(400,500).toFile(toPic);//Change to 400*300, scale according to the original image or put it to 400*a certain height to reduce and enlarge according to the specified ratio / /Scale down and scale Thumbnails.of(fromPic).scale(0.2f).toFile(toPic);// scale down Thumbnails.of(fromPic).scale(2f);// scale up the picture size is not Change, compressed image file size //image size remains unchanged, compressed image file size outputQuality is realized, parameter 1 is the highest quality Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic); I am here Only use the image size unchanged, compressed file size source code
Get currently used configuration file information
example:
Supported processing operations: image scaling, region cropping, watermarking, rotation, maintaining ratio.
It is also worth mentioning that Thumbnailator is still constantly updated, how about it, it feels very secure!
Thumbnailator official website: http://code.google.com/p/thumbnailator/
Here we introduce how to use Thumbnailator
to use the introduction address:
http://blog.csdn.net/chenleixing/article/details/44685817
http://www .qzblog.net/blog/220
http://blog.csdn.net/wangpeng047/article/details/17610451 Thumbnail compressed file jar package <!-- Image thumbnail --> <dependency> <groupId>net.coobird </groupId> <artifactId>thumbnailator</artifactId> <version>0.
</dependency> Scale the picture according to the specified size (it will follow the original picture height and width ratio) //Scale and resize the picture according to the specified size (will follow the original picture height and width ratio) //Here, the picture is compressed to 400× 500 thumbnail Thumbnails.of(fromPic).size(400,500).toFile(toPic);//Change to 400*300, scale according to the original image or put it to 400*a certain height to reduce and enlarge according to the specified ratio / /Scale down and scale Thumbnails.of(fromPic).scale(0.2f).toFile(toPic);// scale down Thumbnails.of(fromPic).scale(2f);// scale up the picture size is not Change, compressed image file size //image size remains unchanged, compressed image file size outputQuality is realized, parameter 1 is the highest quality Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic); I am here Only use the image size unchanged, compressed file size source code
/** * * @Description: save image and generate thumbnail * @param imageFile image file * @param request request object * @param uploadPath upload directory * @return */ public static BaseResult uploadFileAndCreateThumbnail(MultipartFile imageFile,HttpServletRequest request,String uploadPath) { if(imageFile == null ){ return new BaseResult(false, "imageFile cannot be empty"); } if (imageFile.getSize() >= 10*1024*1024) { return new BaseResult(false, "The file cannot be larger than 10M"); } String uuid = UUID.randomUUID().toString(); String fileDirectory = CommonDateUtils.date2string(new Date(), CommonDateUtils.YYYY_MM_DD); // splicing background file name String pathName = fileDirectory + File.separator + uuid + "." + FilenameUtils.getExtension(imageFile.getOriginalFilename()); // Build and save the file path //2016-5-6 yangkang modified the upload path to the server String realPath = request.getServletContext().getRealPath("uploadPath"); //Get the absolute path of the server linux server address Get the currently used configuration file configuration //String urlString=PropertiesUtil.getInstance().getSysPro("uploadPath"); // splicing file path String filePathName = realPath + File.separator + pathName; log.info("Picture upload path: "+filePathName); //Check whether the file save exists File file = new File(filePathName); if (file.getParentFile() != null || !file.getParentFile().isDirectory()) { //Create a file file.getParentFile().mkdirs(); } InputStream inputStream = null; FileOutputStream fileOutputStream = null; try { inputStream = imageFile.getInputStream(); fileOutputStream = new FileOutputStream(file); // write out the file //2016-05-12 yangkang changed to increase the cache // IOUtils.copy(inputStream, fileOutputStream); byte[] buffer = new byte[2048]; IOUtils.copyLarge(inputStream, fileOutputStream, buffer); buffer = null; } catch (IOException e) { filePathName = null; return new BaseResult(false, "Operation failed", e.getMessage()); } finally { try { if (inputStream != null) { inputStream.close(); } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } catch (IOException e) { filePathName = null; return new BaseResult(false, "Operation failed", e.getMessage()); } } // String fileId = FastDFSClient.uploadFile(file, filePathName); /** * Thumbnail begin */ // splicing background file name String thumbnailPathName = fileDirectory + File.separator + uuid + "small." + FilenameUtils.getExtension(imageFile.getOriginalFilename()); //added by yangkang 2016-3-30 Remove the .png string contained in the suffix if(thumbnailPathName.contains(".png")){ thumbnailPathName = thumbnailPathName.replace(".png", ".jpg"); } long size = imageFile.getSize(); double scale = 1.0d ; if(size >= 200*1024){ if(size > 0){ scale = (200*1024f) / size ; } } // splicing file path String thumbnailFilePathName = realPath + File.separator + thumbnailPathName; try { //added by chenshun 2016-3-22 Comment out the previous method of length and width and use size instead // Thumbnails.of(filePathName).size(width, height).toFile(thumbnailFilePathName); if(size < 200*1024){ Thumbnails.of(filePathName).scale(1f).outputFormat("jpg").toFile(thumbnailFilePathName); }else{ Thumbnails.of(filePathName).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(thumbnailFilePathName); } } catch (Exception e1) { return new BaseResult(false, "Operation failed", e1.getMessage()); } /** * Thumbnail end */ Map<String, Object> map = new HashMap<String, Object>(); //Original image address map.put("originalUrl", pathName); // Thumbnail address map.put("thumbnailUrl", thumbnailPathName); return new BaseResult(true, "Operation succeeded", map); }
Get currently used configuration file information
/** * Obtain configuration information from the gzt.properties configuration file according to the key * @param key key value * @return */ public String getSysPro(String key){ return getSysPro(key, null); } /** * Obtain configuration information from the gzt.properties configuration file according to the key * @param key key value * @param defaultValue default value * @return */ public String getSysPro(String key,String defaultValue){ return getValue("spring/imageserver-"+System.getProperty("spring.profiles.active")+".properties", key, defaultValue); }
example:
//Get the server absolute path linux server address String urlString=PropertiesUtil.getInstance().getSysPro("uploadPath"); PropertiesUtil class package com.xyz.imageserver.common.properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; /** * * @ClassName PropertiesUtil.java * @Description System configuration tool class * @author caijy *@date June 9, 2015 at 10:50:38 AM * @version 1.0.0 */ public class PropertiesUtil { private Logger logger = LoggerFactory.getLogger(PropertiesUtil.class); private ConcurrentHashMap<String, Properties> proMap; private PropertiesUtil() { proMap = new ConcurrentHashMap<String, Properties>(); } private static PropertiesUtil instance = new PropertiesUtil(); /** * Get the singleton object * @return */ public static PropertiesUtil getInstance() { return instance; } /** * Obtain configuration information from the gzt.properties configuration file according to the key * @param key key value * @return */ public String getSysPro(String key){ return getSysPro(key, null); } /** * Obtain configuration information from the gzt.properties configuration file according to the key * @param key key value * @param defaultValue default value * @return */ public String getSysPro(String key,String defaultValue){ return getValue("spring/imageserver-"+System.getProperty("spring.profiles.active")+".properties", key, defaultValue); } /** * Get the corresponding key value from the configuration file * @param fileName configuration file name * aramparam key key * @param defaultValue default value * @return */ public String getValue(String fileName,String key,String defaultValue){ String val = null; Properties properties = proMap.get(fileName); if(properties == null){ InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName); try { properties = new Properties(); properties.load(new InputStreamReader(inputStream,"UTF-8")); proMap.put(fileName, properties); val = properties.getProperty(key,defaultValue); } catch (IOException e) { logger.error("getValue",e); }finally{ try { if (inputStream != null) { inputStream.close(); } } catch (IOException e1) { logger.error(e1.toString()); } } }else{ val = properties.getProperty(key,defaultValue); } return val; } }